Sub topic 8
Stopping runs from overlapping
This chapter assumes you can write short shell scripts and know what a file descriptor is. It also assumes you have read the earlier chapter on timeouts and exit codes. Here you will learn how to stop a second run from starting while the first is still going.
Why overlap happens
A scheduler starts jobs on the clock. It does not know whether the last run has finished. Agent runs vary a lot in length, so overlap is common:
- A nightly run takes longer than expected and is still active when the next trigger fires.
- You start a manual test run while the scheduled one is active.
- A retry or a catch-up run (for example
Persistent=truein systemd) lands on top of a normal run.
Two agents editing the same checkout can overwrite each other's files, create conflicting branches, and double your cost. A timeout limits how long a run can last, but it does not prevent overlap. Use both.
Use flock, not a PID file
A PID file has a race. Two scripts can both see "no file" and both create one. They also leave stale files behind after a crash or reboot.
flock asks the kernel for a lock on a file. The kernel releases the lock when the process exits, even after kill -9, so no lock goes stale. The wrapper script from this tutorial uses this pattern:
exec 9>"$HOME/.nightly-agent.lock"
if ! flock -n 9; then
echo "previous run still active, skipping" >> "$LOG_DIR/skipped.log"
exit 0
fiexec 9>fileopens the lock file on file descriptor 9 for the life of the script.flock -n 9tries to lock it without waiting (-nmeans non-blocking). It fails immediately if another process holds the lock.- The lock is released when the script exits. Child processes, including the agent, inherit descriptor 9, so the lock holds until they all exit.
You can see the behavior in a terminal:
flock -n /tmp/demo.lock sleep 30 &
sleep 1
flock -n /tmp/demo.lock echo "got lock" || echo "busy, exit status $?"The second command prints busy, exit status 1. After the background sleep ends, the same command prints got lock.
Skip or fail?
The example exits with status 0 when it skips. That treats a skipped run as normal, which suits jobs where the next run will catch up. If a skip means something is wrong, for example a run that hangs for hours, exit non-zero or count the entries in skipped.log. The alerting chapter covers this.
Locks in other schedulers
- systemd: a timer does not start a second copy of a service that is already running. You still want
flockif people also run the script by hand. - GitHub Actions: the
concurrencygroup in the example workflow serializes runs. Withcancel-in-progress: false, a new run waits for the active one. Only one run can wait at a time. A newer pending run replaces an older pending one.
Common errors
- Lock file in
/tmp: some systems clear it at boot or on a timer. Use a stable path in the job's home or state directory. - Different lock paths: the manual script and the scheduled script must name the same file, or they will not exclude each other.
- Multiple machines:
flockonly works on one host. For jobs on several hosts, use a CIconcurrencygroup or a shared lock service. - Missing
flock: macOS does not ship it by default. Install it, or use another locking tool.
Exercise
Your wrapper script uses flock -n 9 and the server crashes in the middle of a run. After reboot, will the next scheduled run be blocked by a stale lock? Why or why not?
Show answer
No. The lock belongs to the process, not the file. When the process died, the kernel released the lock, so the next run acquires it normally. A PID file would need extra cleanup logic.