How do you make sure only one instance of a cron script runs?
AdvancedConceptPackages, Shell & Text Tools
Answer
Use flock on a lock file; the kernel releases the lock automatically when the process dies, so there are no stale PID files.
*/5 * * * * /usr/bin/flock -n /run/lock/sync.lock /opt/scripts/sync.sh
Or inside the script:
exec 9>/run/lock/sync.lock
flock -n 9 || { echo "already running"; exit 0; }
More on Packages, Shell & Text Tools
Compare apt and dnf package management.BeginnerWhat is the difference between `apt upgrade` and `apt full-upgrade`?IntermediateHow do you find which package provides a file or command?BeginnerHow do you pin or hold a package version?IntermediateHow are repositories and their GPG keys configured?AdvancedWhat is the difference between a login shell and an interactive non-login shell?Intermediate