I have this Perl script for monitoring a folder in Linux.
To continuously check for any updates to the directory, I have a while loop that sleeps for 5 minutes in-between successive loops :
while(1) {
...
sleep 300;
}
Nobody on my other question suggested using cron for scheduling instead of a for loop.
This while construct, without any break looks ugly to me as compared to submitting a cronjob using crontab :
0 */5 * * * ./myscript > /dev/null 2>&1
- Is cron the right choice? Are there any advantages of using the while loop construct?
- Are there any better ways of doing this except the loop and cron?
Also, I’m using a 2.6.9 kernel build.
The only reasons I have ever used the
whilesolution is if either I needed my code to be run more than once a minute or if it needed to respond immediately to an external event, neither of which appear to be the case here.My thinking is usually along the lines of:
cronhas been tested by millions and millions of people over decades so it’s at least as reliable as the code I’ve just strung together.Even in situations where I’ve used
while, I’ve still had acronjob to restart my script in case of failure.My advice would be to simply use
cron. That’s what it’s designed for. And, as an aside, I rarely redirect the output to/dev/null, that makes it too hard to debug. Usually I simply redirect to a file in the/tmpfile system so that I can see what’s going on.You can append as long as you have an automated clean-up procedure and you can even write to a more private location if you’re worried about anyone seeing stuff in the output.
The bottom line, though, is that a rare failure can’t be analysed if you’re throwing away the output. If you consider your job to be bug-free then, by all means, throw the output away but I rarely consider my scripts bug-free, just in case.