Plainnotes

Keeping cron jobs from overlapping with flock

2026-02-18 · 2 min readTools

A backup runs every ten minutes, one run takes twenty-five, and the overlap saturates disk IO.

The simplest mutex is flock:

*/10 * * * * flock -n /var/lock/backup.lock /usr/local/bin/backup.sh

-n exits immediately when the lock is held instead of queueing; without it, runs pile up and the situation gets worse.

Keep the lock file somewhere every user can write, and away from paths other tools manage. Services can wrap the same way, or rely on a oneshot unit with a timer.

fuser -v /var/lock/backup.lock shows who holds it.