#/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; }