ferodynamics wrote:
I did a quick look at cron and don't see anything in there to prevent overlap of execution if a task takes too long. I figure getting from 8 days to 8 years won't be that hard with PHP--and understanding what causes PHP to terminate is probably useful to know anyway.
There are several tricks that you can use to address this issue.
The easiest way would be to create a small "lock" file when your PHP script begins. Then register a shutdown function where you delete the lock file. Every time you run the script from cron, check if the lock file exists. If it does, the previous invocation hasn't completed yet, so just skip this cron job and try again next time.
The problem with this approach is that, if the PHP script doesn't shut down properly, the lock file won't be deleted and none of the subsequent cron jobs will get anything done. There are some ways to fix this, too: for example, if you use Memcached, you could make the lock file expire after a few hours.
Another approach would be to use a "queue". Whenever you have a new job that needs to be done, add it to a queue. You could use something like
Redis to manage the queue, or just query whatever database you're using. Write a PHP script that takes a few items off the queue, processes them, and exits promptly. If there's nothing in the queue, exit immediately. Also write a bash script that keeps calling that PHP script in a loop, maybe with a few seconds of sleep in between. Run the bash script at startup. That way, your queue will always be handled in time, and PHP won't need to sleep. (Sleeping in bash is much more reliable than sleeping in PHP. Anything you write in PHP should just do the job and get the hell out of there.) I used this trick once in the past because one of the PHP libraries I was using had a bad memory leak.
Also, instead of dumping everything to /dev/null, try pointing the output to a file. That way, you'll be able to see any unexpected errors which might cause your script to hang. (By the way, it's
> dev/null, not
< /dev/null. The arrow points in the direction of dumping.)
Bottom line: PHP really isn't a good language for batch jobs from the CLI. Nowadays, I use Python for that.