in reply to Pause Loop

How accurate do you want the delay to be? If you embed a 1 hour delay in the loop along with the commands that are being run, your script will run every 1 hour + however long the commands take, so your schedule will slip continuously. A slightly different formulation of the loop will account for that without having to accurately time how long the commands take:

my $endTime = time() + 3600; while( 1 ) { # Do stuff, # Do more stuff # And more # And more # And more # And more sleep 1 while time() < $endtime; $endtime = time() + 3600; }

The inner sleep loop will consume negligible cpu but will ensure that you don't sleep longer than you need to, even if the active part of the script varies in how long it takes to run.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Pause Loop
by fenLisesi (Priest) on Apr 28, 2007 at 10:07 UTC
    while (1) { my $start_time = time(); ## do stuff here sleep 3600 - time() + $start_time; }
Re^2: Pause Loop
by salva (Canon) on Apr 28, 2007 at 09:24 UTC
    TIMTOWTDI:
    while (1) { # Do stuff here; my $next = int((time + 3599)/3600) * 3600; sleep ($next - time); }
        why not?

        unless I am overlooking something obvious, my sample code would run "stuff" at every hour, on the hour.

        Well, except the first time, that would run inmediately, but that can be changed easily:

        my $first = time; while (1) { # Do stuff here; my $next = int((time - $first + 3599)/3600) * 3600; sleep ($first + $next - time); }