Prevent overlapping cron jobs with flock
cron job running multiple times at once | prevent cron job overlap | lock file for cron job | run script only once at a time | flock | cron | lockflock -n /tmp/backup.lock /usr/local/bin/backup.shflock holds a lock on the lock file while the command runs, and -n aborts immediately if the lock is already taken. So if yesterday’s backup is still running, the new run does not start at all. In the crontab, simply put it in front of the actual command, for example 0 3 * * * flock -n /tmp/backup.lock /usr/local/bin/backup.sh.
Note: The lock is released automatically when the process ends or crashes, so a stale lock file does not block anything.
Also searched as
- cron job running multiple times at once
- prevent cron job overlap
- lock file for cron job
- run script only once at a time