Contributed by Novician on Jul 26, 2000 at 12:34 UTC
Q&A  > programs and processes


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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.