develooper Front page | perl.beginners | Postings from November 2002

RE: how do i make a script run for a certain period of time?

Thread Previous | Thread Next
From:
Jose.NYIMI
Date:
November 28, 2002 01:34
Subject:
RE: how do i make a script run for a certain period of time?
Message ID:
C5DF5DEDC8BD6B46AB93D735D4C47A980DA0AB@edcb570.nt.mobile.belgacom.be
I'm writing a script that has to be automatically
executed every day at 6:00 AM via 'dollar universe' scheduler software.
I want my script to stop itself at 23:00 PM
(I think to timeout via alarm function).
The script is mainly a while(1) loop and I want my loop
to sleep few seconds before going to the next iteration.
How can handle this without mixing alarm and sleep ?

Am I "bad bad guy" :-) doing the following ?

#!/usr/bin/perl -w

use strict;
use MyClass;

my $obj=MyClass->new();

my $time_to_die=0;
my $start=6; #process starts at 6:00 AM
my $end=23;  #process ends at 23:00
my $timeout=($end-$start)*3600; #in seconds
my $sleep=5;

$SIG{ALRM} = sub { $time_to_die=1; };

alarm($timeout);
 
#-----
#Main
#-----
eval{
  while(!$time_to_die){
    #get_request() retrieves request's info from an Oracle table.
    $obj->get_request() and $obj->run();
    sleep($sleep);
  }  
  die "got_time_out";
};
if($@){
  if($@=~/got_time_out/){
    exit(0);
  }
  else{
    alarm(0);#clear the still-pending alarm
    $obj->report_failure($@);
    exit -1;
  } 
}
__END__

> -----Original Message-----
> From: david [mailto:dzhuo@looksmart.net] 
> Sent: Wednesday, November 27, 2002 8:59 PM
> To: beginners@perl.org
> Subject: RE: how do i make a script run for a certain period of time?
> 
> 
> Nyimi Jose wrote:
> 
> >> -----Original Message-----
> >> From: david [mailto:dzhuo@looksmart.net]
> >> Sent: Wednesday, November 27, 2002 8:34 PM
> >> To: beginners@perl.org
> >> Subject: RE: how do i make a script run for a certain 
> period of time?
> >> 
> >> 
> >> Nyimi Jose wrote:
> >> 
> >> > If you want your loop to first finish
> >> > what he is doing before die, do this instead.
> >> > Thus,when you receive the alarm sig you exit
> >> > first the loop then the script.
> >> > 
> >> > 
> >> > #!/usr/bin/perl -w
> >> > 
> >> > 
> >> 
> >> you must be a bad bad guy!!! :-) just kidding!
> > 
> > Sorry !
> > 
> >> never listen to the perldoc hua? don't ever mix sleep and 
> alarm! on 
> >> most platform, sleep is implemented using alarm call. the problem? 
> >> the alarm call inside the sleep function will thus replace your
> >> alarm function
> >> just outside the while loop. check the perldoc -f alarm for more.
> > 
> > What about this ?:
> > 
> > use strict
> > my $time_to_die=0;
> > my $timeout=30; #in seconds
> > 
> > $SIG{ALRM} = sub { $time_to_die=1; };
> > 
> > alarm($timeout);
> > while(!$time_to_die){
> > #tasks here
> > }
> > __END__
> > 
> 
> much better. a bit different than my first post. your code 
> will at least 
> execute the whole block of the while loop. my original post 
> will die in the 
> middle. if he never want to die in the middle of the while 
> loop even the 
> alarm expires, your solution is better. if he want to die as 
> soon as the 
> alarm expires, my solution is bit better.
> 
> david
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> 
> 


**** DISCLAIMER ****

"This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer".

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About