Automating background maintenance routines, database backups, API sync tasks, and log parsing via cron is a cornerstone of Linux system administration. However, scheduled tasks carry an inherent vulnerability: execution overlap. If a scheduled script takes longer to finish than its scheduled frequency interval (for example, a 5-minute task delayed by heavy network latency or massive database locks), cron starts another instance concurrently.
Concurrent execution of non-reentrant scripts causes catastrophic race conditions, database deadlocks, high CPU loads, and corrupted files. While many administrators attempt to solve this by creating manual lockfiles (such as testing if [ -f /tmp/lock ]; then exit; fi), manual lockfiles are fragile and fail to clean up when a process crashes, leaving the system in a permanently locked state.
The standard, bulletproof Unix solution is the flock utility (part of the core util-linux package). flock utilizes kernel-level file descriptor locking (the flock(2) system call). The kernel automatically releases the lock the instant the process exits, even if killed abruptly with SIGKILL. This tutorial explains how to use flock in crontab definitions and inside Bash shell scripts.
Method 1: Wrapping Cron Jobs Directly with flock
The most common implementation wraps scheduled cron commands directly inside the crontab table without modifying the underlying script.
- Open your user or root crontab editor:
crontab -e - To run a backup script every 10 minutes while guaranteeing that only one instance runs at any given moment, structure the entry as follows:
*/10 * * * * /usr/bin/flock -n /tmp/backup.lockfile /opt/scripts/backup.sh
Breaking down the command arguments:
/usr/bin/flock: Explicit absolute path to the flock binary (ensuring path resolution under cron’s minimal environment).-n(or--nonblock): Non-blocking mode. If another instance currently holds the lock on/tmp/backup.lockfile, flock immediately exits with status code1instead of waiting in an endless queue./tmp/backup.lockfile: The target lockfile managed by the Linux kernel./opt/scripts/backup.sh: The actual target script or command to execute under lock protection.
Waiting with a Timeout Instead of Immediate Exit
If you want a delayed script to wait briefly for a previous run to complete before aborting, specify a timeout in seconds with -w:
0 * * * * /usr/bin/flock -w 60 /tmp/hourly-sync.lock /opt/scripts/sync.sh
In this example, flock will wait up to 60 seconds for the lock to become available. If the lock clears within 60 seconds, it acquires the lock and runs; otherwise, it exits cleanly.
Method 2: Integrating flock Directly Inside a Bash Script
If your script is triggered from multiple different sources (such as webhooks, manual terminal executions, and automated timers), embedding lock management directly within the script guarantees protection regardless of how it is invoked.
Add the following boilerplate pattern to the top of your Bash script:
#!/usr/bin/env bash
# Define lockfile location
LOCKFILE="/var/run/my_task.lock"
# Open file descriptor 200 pointing to the lockfile
exec 200>"$LOCKFILE"
# Attempt to acquire an exclusive non-blocking lock on file descriptor 200
if ! flock -n 200; then
echo "[$(date)] Another instance of this script is already running. Aborting." >&2
exit 1
fi
# Everything past this line executes exclusively under lock protection
echo "[$(date)] Lock acquired. Starting critical processing..."
# Simulate workload
sleep 30
echo "[$(date)] Processing finished. Exiting."
Why File Descriptor Locking Is Superior
- Automatic Cleanup: You do not need to register
trap "rm -f $LOCKFILE" EXITsignals. When the script exits or terminates abnormally, the Linux kernel closes file descriptor 200 and unlocks the file instantly. - Race-Condition Proof: The creation and locking of the file descriptor are atomic operations handled in kernel space, eliminating time-of-check to time-of-use (TOCTOU) file bugs.
Inspecting and Testing Active Locks
To verify which process currently holds a lock or diagnose stuck jobs, inspect open file locks using lslocks:
sudo lslocks | grep lockfile
The command lists the process ID (PID), command name, lock type (FLOCK), access mode (WRITE), and path of the held file lock, allowing you to monitor job execution safely.