Love, caffeine and omlette

How to prevent a cron job from running multiple times

Sometimes a cron job might run slower, than you anticipated and many instances of it stack up.

You can increase the time interval during starts of the cron job - this can lead to problems in the future however.

Fortunately there's a easy and clean fix.

Just replace your cronjob with this:

/usr/bin/flock -n /var/lock/<foobar> <long-running-task>

Whenever your cron job is started a lock file will be created and no other instance can start.


This a simple remedy for colliding cron job's. I use it frequently.

1 Comment

  1. Gwyneth Llewelyn

    February 13, 2014 at 2:47 am

    Thanks! So simple, but effective! I always wondered how this was done, since I saw some hosting providers who offered this "protection" and was a bit clueless on how to implement it...

Leave a Reply

Your email address will not be published.


© 2017

Theme by Anders NorenUp ↑