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 27, 2002 11:18
Subject:
RE: how do i make a script run for a certain period of time?
Message ID:
C5DF5DEDC8BD6B46AB93D735D4C47A980DA0A9@edcb570.nt.mobile.belgacom.be
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

use strict
my $time_to_die=0;
my $sleep=5; #in seconds
my $timeout=3600; #in seconds

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

alarm($timeout);
while(!$time_to_die){
	#tasks here 
    	sleep($sleep);
}  
__END__


José.

> -----Original Message-----
> From: david [mailto:dzhuo@looksmart.net] 
> Sent: Wednesday, November 27, 2002 7:50 PM
> To: beginners@perl.org
> Subject: Re: how do i make a script run for a certain period of time?
> 
> 
> Nix Juban wrote:
> 
> > i want to insert this script in one of my programs 
> appearing like its 
> > processing or doing something..since my loop is while(1), meaning 
> > always true, it will run forever until i halt it. 1)How do 
> I time it 
> > to run for only few seconds?
> > 2) What if im really processing something like extracting 
> data through 
> > ftp and parsing it, how do i run this while its doing all that?
> > 
> 
> try the alarm function along with $SIG{ALRM}. example:
> 
> #!/usr/bin/perl -w
> use strict;
> 
> $SIG{ALRM} = sub { die "see ya\n" };
> 
> alarm(2);
> while(1){
>         print time,"\n";
> }
> 
> __END__
> 
> the above will end in about 2 seconds. check:
> 
> perldoc -f alarm
> 
> for more. HTH
> 
> 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