sidsinha has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a functionality written in perl but I want to run this function once every specific time of an hour. For example, every hour at x:20 (like 10:20, 11:20, 12:20, 1:20...). I need to build in intelligence in a way that even if i run at 10 AM, it should poll at 10:20 and likewise i trigger the script at 9:30, the next poll interval is still 10:20. Any suggestions? I am doing this in windows. Regards, Sid

Replies are listed 'Best First'.
Re: Script to poll every hour
by kennethk (Abbot) on Jul 18, 2013 at 20:44 UTC
    This sounds more like something that should be solved via a cron job, particularly since it sounds like something that should be persistent. The Windows equivalent is using the scheduler, which is found via
    Start > Programs > Accessories > System Tools > Task Scheduler
    or something like that, depending on your version.

    That is, of course, not the question you asked.

    To run a script and then pause it until 20 past the hour, you should use localtime to get the minutes (my $min = (localtime)[1];) and then sleep 60 * the mod of the difference between that and the target (sleep(60 * ((20 - $min) % 60))). If you need better precision, you can include seconds (or even milliseconds) in that calculation.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thankyou Sir, exactly what I wanted!!
Re: Script to poll every hour
by marinersk (Priest) on Jul 18, 2013 at 23:06 UTC
    Here's a working example of how to get the current time and how to make your Perl script wait a specified amount of time. You should be able to do the rest on your own. :-)
    #!/usr/bin/perl -w use strict; my %TRUE_FALSE = ( '1' => 'True', '0' => 'False', ); my %DAY_OF_WEEK = ( '0' => 'Sunday', '1' => 'Monday', '2' => 'Tuesday', '3' => 'Wednesday', '4' => 'Thursday', '5' => 'Friday', '6' => 'Saturday', ); { &displayTime(); &waitAwhile(3); &displayTime(); } exit; sub displayTime { my $nowsse = time; my ($nowsec,$nowmin,$nowhou,$nowdom,$nowmon,$nowyea,$nowdow,$nowdo +y,$nowdst) = localtime($nowsse); $nowyea += 1900; $nowmon++; print "---------------------------------------------\n"; my $nowDisplay = sprintf "%04d-%02d-%02d\@%02d:%02d:%02d", $nowyea, $nowmon, $nowdom, $nowhou, $nowmin, $nowsec ; print " Time right now: $nowDisplay\n"; print " Day of Week: $DAY_OF_WEEK{$nowdow}\n"; print " Day of Year: $nowdoy\n"; print "Daylight Savings: $TRUE_FALSE{$nowdst}\n"; print "---------------------------------------------\n"; } sub waitAwhile { my ($waitSeconds, @dummy) = @_; if (!defined $waitSeconds) { $waitSeconds = 0; } if ($waitSeconds <= 0) { $waitSeconds = 3; } print "Waiting $waitSeconds seconds\n"; sleep $waitSeconds; } __END__ C:\Steve\Dev\PerlMonks\P-2013-07-18@1650-Time-Computation>perl timeCom +putation.pl --------------------------------------------- Time right now: 2013-07-18@17:03:10 Day of Week: Thursday Day of Year: 198 Daylight Savings: True --------------------------------------------- Waiting 3 seconds --------------------------------------------- Time right now: 2013-07-18@17:03:13 Day of Week: Thursday Day of Year: 198 Daylight Savings: True ---------------------------------------------
    Have fun!
      Awesome, I could use this in my script too. THanks a lot
Re: Script to poll every hour
by hdb (Monsignor) on Jul 18, 2013 at 19:53 UTC

    Calculate the time in seconds until the desired next poll time and put your script to sleep for that many seconds.