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

Hey Monks, I'm a moderate skilled perl person trying to mimic the 'cron' functionality of *nix systems without using any modules. What I'm doing is fork a child and running an infinite loop:
my $interval = 300; my $cpid = fork(); my $upto = localtime() + $interval; while (1) { if ($upto == localtime()) { $upto = localtime() + $interval; system("/home/rambo/perl-assignments/send_test_mail.pl"); } sleep 1; }
My question is how can I do this if I have three or more jobs which runs at different intervals? Can it be possible to put above in a sub and do a foreach of different intervals/scripts to be executed? I'm thinking of a timer kind of thing which forks(parallel forks if more), execs respective script and kills child after.

Replies are listed 'Best First'.
Re: cron functionality mimic in perl scripting
by Corion (Patriarch) on Sep 20, 2011 at 11:46 UTC
Re: cron functionality mimic in perl scripting
by moritz (Cardinal) on Sep 20, 2011 at 11:46 UTC
Re: cron functionality mimic in perl scripting
by jwkrahn (Abbot) on Sep 20, 2011 at 12:34 UTC
    if ($upto == localtime()) { $upto = localtime() + $interval;

    localtime returns a string in scalar context and you can't properly do arithmetic operations on a string.

    $ perl -le'print scalar localtime' Tue Sep 20 05:32:46 2011

    Perhaps you meant to use time instead?

Re: cron functionality mimic in perl scripting
by jethro (Monsignor) on Sep 20, 2011 at 12:01 UTC

    I suspect that cron just has a ordered list of times the different jobs have to be started and checks every second if the first entries in the list have to be started. It is a bit inefficient to have a process hanging around for every single job in the queue

Re: cron functionality mimic in perl scripting
by zentara (Cardinal) on Sep 20, 2011 at 15:47 UTC
    My question is how can I do this if I have three or more jobs which runs at different intervals?

    You would be better off using an event-loop system, many to choose from, and setup timers.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh