Created
April 3, 2017 15:31
-
-
Save tsaavik/11499b9ec281f03c6a3683d526f4fc14 to your computer and use it in GitHub Desktop.
Atomic locking for bash scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
lockfile=/var/lock/$(basename $0) | |
#Redirects output of file descriptor 5 to lockfile while this script runs | |
exec 5>"${lockfile}" | |
# flock file descriptor 5, otherwise fail | |
if ! flock -n 5 ; then | |
echo "Exiting: Another instance of $0 is already running"; | |
exit 1 | |
fi | |
# We now have the lockfile until this script exits |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment