Add new comment

Locking Cron Jobs

Some cron jobs have the potential for running longer than their allotted interval, and you don't want to end up running multiple copies, especially where that creates additional load meaning the jobs take even longer to complete. That's a sure-fire recipe for disaster.

Historically I've always coded this into a shell script to use "ps" and exit if a copy of the job is already running. That works, but it's a pain to reimplement it every time. Somehow I missed that there is a tool built exactly for this specific function, named flock.

As a simple example:

flock -xn /tmp/lockfile -c /path/to/script

Hat tip to https://bencane.com/2015/09/22/preventing-duplicate-cron-job-executions/.

Subscribe to zmonkey.org - All comments