Cron starts your command at the scheduled time and doesn't care whether the previous run has finished. If a job that normally takes 3 minutes runs every 5 minutes and one day takes 8, you get two copies running at once. The simplest fix on Linux is to wrap the command in flock:
*/5 * * * * flock -n /var/lock/report.lock /opt/scripts/report.sh
With -n (non-blocking), a second invocation sees the lock is held and exits immediately instead of running.
Why overlaps happen
Cron has no concept of a job's state. It forks the command and moves on. Overlaps usually come from:
- A slow dependency, such as a database under load, an API that's timing out or a large backup.
- Data growth. A job that took 40 seconds a year ago takes 6 minutes now and still runs every 5.
- A hung process that never exits, so every later run piles on top of it.
- A fast schedule combined with a job that has no upper bound on its runtime.
The damage depends on the job. Two backups can fill the disk, two importers can insert duplicate rows, and two report generators fight over the same temp file. Stacked processes also compound the slowdown that caused the overlap, since each extra copy competes for the same resources.
flock basics
flock is part of util-linux and is installed on practically every Linux distribution. It takes an advisory lock on a file and runs your command while holding it. When the command exits, the lock is released by the kernel, even if the process was killed or crashed.
# Skip this run if another one holds the lock
flock -n /var/lock/report.lock /opt/scripts/report.sh
# Wait up to 60 seconds for the lock, then give up
flock -w 60 /var/lock/report.lock /opt/scripts/report.sh
# Wait indefinitely (the default); runs queue up behind each other
flock /var/lock/report.lock /opt/scripts/report.sh
Which one you want depends on the job:
-nis the right choice for "if the last one is still going, skip this one". Most periodic jobs want this.-w SECONDSmakes the new run wait a limited time. Use it when the work must happen but a short delay is fine.- Without either, runs queue up forever. This can build a backlog of blocked processes if a job hangs, so avoid it in cron.
By default flock returns exit status 1 when it can't get the lock with -n. Cron may email you about that if the command prints anything or if you rely on exit codes in a wrapper. Use -E to pick a different code, or exit quietly:
flock -n -E 0 /var/lock/report.lock /opt/scripts/report.sh
With -E 0, a skipped run counts as success. That's usually what you want for a "skip if busy" job.
Where to put the lock file
The file is only a handle; its contents don't matter, and it can safely stay on disk between runs. Choices:
/var/lock/(usually a symlink to/run/lock) is the conventional place for system jobs. It's cleared on reboot, which is a good thing.- Regular users often can't write to
/var/lock. Use/tmp/myjob.lockor$XDG_RUNTIME_DIR(/run/user/<uid>/) instead. - Use one lock file per job. If two unrelated jobs share a lock file, one blocks the other.
- Don't put the lock on an NFS or similar network share unless you know your setup supports it. Locking over network file systems can behave differently.
Locking inside a script
For a script that is run from several places, or that you want to protect regardless of who calls it, take the lock inside the script:
#!/bin/bash
exec 200>/var/lock/report.lock
flock -n 200 || { echo "report.sh already running, exiting" >&2; exit 0; }
# --- work starts here ---
/opt/scripts/export-data.sh
/opt/scripts/send-report.sh
exec 200>file opens the file on file descriptor 200 for the lifetime of the script, and flock -n 200 locks that descriptor. When the script exits, the descriptor closes and the lock goes with it. The number 200 is arbitrary, as long as it isn't 0, 1 or 2 and you haven't used it elsewhere.
A subtle point: child processes inherit the file descriptor. If your script starts a background process that outlives it, that process keeps the lock until it exits. That's sometimes useful and sometimes a surprise.
Cap the runtime with timeout
A lock stops new copies, but a hung job holds the lock forever, and every later run is skipped. Pair flock with timeout so a stuck job eventually dies:
*/5 * * * * flock -n -E 0 /var/lock/report.lock timeout 240 /opt/scripts/report.sh
timeout 240 sends SIGTERM after 240 seconds. If the process ignores it, add -k to follow up with SIGKILL:
timeout -k 10 240 /opt/scripts/report.sh
Pick a limit just under the schedule interval, or at the longest run you consider acceptable. When timeout fires it exits with status 124, which is useful to detect in a wrapper and alert on. A job being killed every night is a bug to fix, not a pattern to live with. See monitoring cron jobs for catching that.
The pid-file approach, and why it breaks
Before flock, the usual method was a pid file:
#!/bin/sh
PIDFILE=/tmp/report.pid
if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
exit 0
fi
echo $$ > "$PIDFILE"
trap 'rm -f "$PIDFILE"' EXIT
/opt/scripts/report.sh
It mostly works, and has several weaknesses:
- Stale pid files. If the machine loses power or the process gets SIGKILL, the trap never runs and the file stays. Without the
kill -0check, the job would never run again. - PID reuse. After a reboot or a long time, the old PID may belong to an unrelated process, so
kill -0succeeds and your job is skipped forever. - Race window. Two copies can both pass the check before either writes its PID.
flock avoids all three because the kernel owns the lock and drops it when the process dies. Prefer it unless you're on a system without it. On macOS, flock isn't included by default, and a common alternative is mkdir /tmp/job.lock (which is atomic) with the same stale-lock caveats.
Other schedulers
systemd timers. A timer activates a service unit, and if that service is still running, the timer's next trigger does nothing, so runs don't overlap by default. If you need a stricter guarantee, set RuntimeMaxSec= on the service to cap how long it can run. This is one of the advantages covered in cron vs systemd timers.
Kubernetes. Set concurrencyPolicy: Forbid on the CronJob to skip a run when the previous Job is still active, or Replace to kill the old one and start fresh. Add activeDeadlineSeconds as the runtime cap. Details in the Kubernetes CronJob guide.
Laravel. The scheduler has ->withoutOverlapping(), which uses a cache lock and skips a run if the previous one hasn't finished. The lock expires after 24 hours by default; pass a number of minutes (->withoutOverlapping(10)) so a crashed run doesn't block the task for a day. See cron with PHP and Laravel.
Checklist
- Use
flock -n -E 0for jobs that should skip when busy. - One lock file per job, in
/var/lockor a user-writable directory. - Add
timeoutso a hung run can't hold the lock forever. - Log when a run is skipped, so you notice a job that is always being skipped.
- Check that the schedule interval is longer than the typical run. If it isn't, locking only hides the real problem.