in reply to Re: How to make PERL Script run Automaticly?
in thread How to make PERL Script run Automaticly?

Yes, it will run approximately every 2 hours, although if somesub() take some time to execute, you have to realize that the sleep 7200 will start only after the completion of the sub so that the start time of one start compared to the previous will be offset by 2 hours + the duration of the sub. So, in effect, il will run every 2 hours + sub execution duration. This might or might not matter for you.

A cron job would not suffer from this defect, may be you can ask your sysadmin. One workaround is to have the subroutine to launch the process as a background process, so that the subroutine would take only a couple of second to execute.

Update: fixed two typos in the word duration.

Replies are listed 'Best First'.
Re^3: How to make PERL Script run Automaticly?
by David92 (Sexton) on Jul 04, 2014 at 08:19 UTC
    good point Laurent, thanks! For now, it is not of that matter, because I timed it that the script needs 10minutes to run. Later on, this will come into matter, as the script will grow larger.

    Just to clearify it for myself: So if I run the above code once in Linux .. it will continue running until I close the Linux client, right?

      For now, it is not of that matter, because I timed it that the script needs 10minutes to run. Later on, this will come into matter, as the script will grow larger.

      You could time the function call:

      while(1) { my $startingtime = time; expensive_function(); sleep $startingtime - time() + 7200; }

      This may still introduce drift over time as time only has a granularity of one second, though.

      In any case, using cron (as suggested above) is definitely the right thing to do. Just ask your friendly local sysadmin.

      Yes it will run "forever", i.e. until you kill it, switch off the machine or something else goes wrong. Make sure that you don't have a memory leak (although this is relatively uncommon in Perl), because if you do have one, your program is doomed to fail sooner or later.