How to reliably run a batch job every 5 seconds?

Posted by Benjamin on Server Fault See other posts from Server Fault or by Benjamin
Published on 2012-11-26T11:57:39Z Indexed on 2012/11/28 11:05 UTC
Read the original article Hit count: 208

Filed under:
|

I'm building an application where the sending of all notifications (email, SMS, fax) will be asynchronous. The application will write the notifications to the database, and a batch job will read these notifications and send them with the appropriate transport.

I was first reading at ways to run cron faster than the minute, and realized this was a bad idea.

The batch scripts are written in PHP, and I guess that writing a proper daemon would be quite an overhead (though I'm open to any suggestion, as PHP car run indefinitely as well).

What I have in mind is a solution that would:

  • Run the PHP script every 5 seconds
  • Check that the previous run has finished, or abort (never 2 concurrent batches running)
  • Kill the script if live for more than x minutes (a security in case it hangs)
  • Start with the system (if a reboot occurs)

Any idea how to do this?

© Server Fault or respective owner

Related posts about cron

Related posts about batch-processing