Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

What is the best way to run periodic tasks (once per minute)?

( #24427=categorized question: print w/ replies, xml ) Need Help??
Contributed by Novician on Jul 26, 2000 at 12:34 UTC
Q&A  > programs and processes


Description:

I would like to know how to create a perl programme that will run forever, something like a timer where it will push or copy a certain file to another directory at a certain hour everyday.

The timer i tried before, ran every second. Resource over-utilized is not the biggest concern, its only the second. The thing is that, my timer version runs for every second in that minute. Can i make it run like once in that minute i choose it to run?

my version timer that i used, utilizes a FOR loop. Is there a better way of coding it and also making it run once in that minute i specify.

Answer: What is the best way to run periodic tasks (once per minute)?
contributed by Corion

First of all, most operating systems already have services for scheduling program runs. UNIX has the cron program and Windows NT has the AT program and service. Look first into these before you think of rolling your own.

If you still want to roll your own, look into the sleep() function call, which will put your script to sleep for the amount of time you specify. Note that running such a script on a server without the administrators knowledge could have bad consequences for you. First talk to your admin and think if using cron would do the job for you as well.

Here is a small program that prints a line every 10 seconds (tested) :

#!/usr/bin/perl -w use strict; my $tick = "tick\n"; # loop until this process is terminated from the outside while (1) { print $tick; # Make "tick" into "tock" and "tock" into "tick" $tick =~ tr/io/oi/; # sleep 10 seconds sleep( 10 ); }

Answer: What is the best way to run periodic tasks (once per minute)?
contributed by qi3ber

Corion is correct. A modification that I have had to make on several occasions allows for more time intensive tasks is this:

while (1) { $waketime = time + $sleep_duration; &do_complex_things; sleep($waketime-time); }


Something else that you might want to look into is the Time::HiRes module.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (2)
    As of 2014-09-21 00:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (165 votes), past polls