Limit the number of cron jobs runnging a PHP script

Posted by ed209 on Stack Overflow See other posts from Stack Overflow or by ed209
Published on 2010-05-11T18:31:12Z Indexed on 2010/05/11 18:34 UTC
Read the original article Hit count: 303

Filed under:
|

I have a daily cron job that grabs 10 users at a time and does a bunch of stuff with them. There are 1000's of users that are part of that process every day. I don't want the cron jobs to overlap as they are heavy on server load. They call stuff from various APIs so I have timeouts and slow data fetching to contend with.

I've tried using a flag to mark a cron job as running, but it's not reliable enough as some PHP scripts may time out or fail in various ways.

Is there a good way to stop a cron job from calling the PHP script multiple times, or controlling the number of times it is called, so there are only 3 instances for example?

I'd prefer a solution in the PHP if possible. Any ideas?

At the moment I'm storing the flag as a value in a database, is using a lock type file any better as in here http://stackoverflow.com/questions/851872/does-a-cron-job-kill-last-cron-execution ?

© Stack Overflow or respective owner

Related posts about cron

Related posts about php5