If you want to have a program sleep, and then run something every 15 minutes, you could write:
use strict;
# Loop forever
while(1) {
some_function();
another_function();
# Sleep statement, in seconds
# 15 minutes * 60 seconds in a minute = 900 seconds
sleep 900;
}
sub some_function {
foo;
bar;
}
sub another_function {
yada_yada;
wocka_wocka;
}
However, I am the type who would absolutally prefer to use cron to handle timing, as opposed to Perl. Cron is a wonderful resource, and I find it easier to use an existing resource then to recreate one. It's one thing if you will always stick to 15 minutes.. but what if you ever decide you don't want it to run on the weekends?
Secondly, by having your Perl program handle the time -- if you ever reboot, you'll need to rerun the program. Or create a startup script to do it for you. What if you end up with 15 of these scripts? Or 50? I would personally find it much easier to manage them with cron.
Lastly, a point could be raised about system resources. Having scripts running all the time uses memory. It may be easier on your system resources to have them only run when necessary. The other side of the coin is that depending on the script, it might take a lot of CPU time to fire up that script every 15 minutes, instead of leaving it in RAM where the system may cache it. But you'll have to do some benchmarks to work that out :-)
-Eric | [reply] [d/l] |
| [reply] |
<minor nitpick>
Your code does not execute some_function() and another_function() every 15 minutes, it puts a 15 minute pause in between, if each function took 5 minutes then it would run them every 25 minutes. To execute them every 15 minutes you would need to check the time() at the top of the loop and again at the bottom and adjust the sleep time accordinly which could still drift your loop by a fraction of a second with each itteration. I do second... errr fourth your suggestion to use cron.
</minor nitpick>>
| [reply] |
Of course cron might be the best way, but if you have a kind of server-program, which
is always running, you could use alarm to send a alarm signal to your program.
Then you can call a subfunction when the alarm signal is received.
i.e.
alarm 900; #send alarm after 900 sec
$SIG{ALRM} = \&alarm_fnct;
# Here should be the code of your allways running server
sub alarm_fnct {
print "Get alarm Signal";
...
alarm 900; ## and again call alarm after 900 sec.
}
Remember this is only usefull, if you have a kind of server program, and you want to call every 900 sec. a special sub,
i.e. to read the contents of a directory or something like that.
Hope this is not 'off topic' :)
-----------------------------------
--the good, the bad and the physi--
-----------------------------------
| [reply] [d/l] [select] |