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:
david
Date:
November 30, 2002 19:51
Subject:
RE: how do i make a script run for a certain period of time?
Message ID:
20021201035123.23505.qmail@onion.perl.org
Nyimi Jose wrote:

> 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 ?
> 

you have a few options:

1. just mix alarm and sleep. Not all sleep functions are implemented using 
alarm. If yours doesn't, you won't have to worry about it.

2. use the 4 argument version of the select() function with the first three 
arguments being undef so you can delay any time you want. you can even get  
finer granularity than a second if your system supports it.

3. Implement your own "sleep" function. Not really recommanded but I have 
seen people doing things like:

#!/usr/bin/perl -w
use strict;

while(1){
        delay(3);
        #-- task
}

sub delay{
        my $time = $_[0] * 999999;
        for(my $i = 0; $i < $time; $i++){} 
}

__END__

which is NOTHING close to the sleep function and it's wasting your system's 
CPU resource. it merely slows down the program in a bad way!

4. check perldoc if you haven't do so. I am sure there are other methods 
that works better since this is a fairly command task. There might even be 
a module in CPAN that deals with that.

david

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