> > Hi all, > > I'm writing a script which fetches data every hour. I thought instead of > using cron which is platform dependent, to use sleep and a goto statement. Is > there any downfalls to this?
Sure, all of the differences between a one-off script and a constantly running program. cron is platform dependent, but just about every platform (at least where Perl runs) has a scheduler of some sort, and cron is available on most of them. I am not saying not to do it, just make sure you do it for the right reasons and get the mileage you expect. > At the start of the script I check to see if it was ran in the previous hour. > > BEGINNING: > if(open(TIMECHECK, "./synop_daemon_timer.txt")){ > my($cur_sec1,$cur_min1,$cur_hour1,$cur_day1,$cur_mon1) = <TIMECHECK>; > my($cur_sec,$cur_min,$cur_hour,$cur_day,$cur_mon,$cur_year)=gmtime(time()); > if($cur_hour1 eq $cur_hour && $cur_day1 eq $cur_day){ > print "<br> SCRIPT IS ALREADY IN ACTION, CANNOT CONTINUE"; Already in action? You mean has already run. Big difference, at least when you are talking about making a jump like this. What happens during daylight savings time, assuming your platform has such a thing? What happens if the system time changes, do you run ntpd? > exit(0); > } > close(TIMECHECK); > } > > # at the end of the script I write the last time the script was started > if(open(TIMER, ">./synop_daemon_timer.txt")){ > 'started' or 'ran'... big difference again. > my($cur_sec,$cur_min,$cur_hour,$cur_day,$cur_mon,$cur_year,$junk)=gmtime(time()); > print TIMER "$cur_sec,$cur_min,$cur_hour,$cur_day,$cur_mon\n"; > close(TIMER); > } > sleep(360); This sleeps for 6 minutes from whenever the last iteration finished, this is a very different thing then when running with a scheduler which is going to run at specific times. aka if you start your script at 12:02 then it runs at 12:08, etc. However, if your script starts at 12:03, then it runs at 12:09, etc. A scheduler will make sure it runs consistently, because it doesn't depend on when it was started (aka because it isn't constantly running). Add to this the dependency on the run time, aka if the processing itself takes a while or is dependent on external uncontrollable forces, such as network speed. Maybe your script starts at 12:04, the first loop takes 2 minutes to run, then your script will run again at 12:12, but this time takes 4 minutes to run, so the next run starts at 12:22, so much for consistency :-).... > goto(BEGINNING); > Speaking of constantly running, are you going to monitor that the process is still alive. How about adding it to the init functions of the computer, in other words, cron is started at boot time, and just runs your script at the next interval. A constantly running program would need to be added to the platform's boot sequence. Can anyone reboot the machine, aka are the sys admin's aware of your app? You may also want to check out Proc::Daemon, are you going to redirect STDIN/STDERR/STDOUT to a log? How about disassociating the process from the terminal? Also consider an 'alarm' call, though I am not sure whether it is considered better or worse than a 'sleep'. perldoc -f alarm Lots to think about, I would caution you to at least think about all of the things that could go wrong with this approach. But again, I am not telling you not to do it.... http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>