#/usr/bin/env perl use strict; use warnings; # We need sub-second precision here :-) use Time::HiRes wq(time sleep); my $lastrun = 0; while(1) { # Time consuming stuff here my $now = time; # Calculate the time we need to sleep. First we calculate the difference between now and the last run. # That's how long the last run took. Now, calculate how many seconds remaining in the current minute. # If the answer is negative, one of two things happened: Either it's our first run, or the last run took # longer than a minute my $sleeptime = 60 - ($now - $lastrun); if($sleeptime > 0) { sleep($sleeptime); } $lastrun = $now; } #### #/usr/bin/env perl use strict; use warnings; # We need sub-second precision here :-) use Time::HiRes wq(time sleep); my $activesecond = 42; # Run whenever the seconds are "42" while(1) { my $now = time; # The built-in modulus function converts to integer, # which would introduce jitter of up to nearly a second. # So, out with the traditional: # my $cursecond = $now % 60; # ...and in with the more manual version: my $cursecond = $now - (60.0 * int($now / 60.0)); if($cursecond != $activesecond) { # Need to wait my $sleeptime = $activesecond - $cursecond; if($sleeptime < 0) { # Handle rollover $sleeptime += 60.0; } sleep($sleeptime); } # Time consuming stuff here } #### my $taioffset = 37; ... my $now = $time + $taioffset;