Created
March 18, 2016 09:38
-
-
Save eripa/0a8a4892a2a2a6f6f508 to your computer and use it in GitHub Desktop.
shell wrapper lockfile
This file contains hidden or 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 | |
set -euo pipefail | |
lockfile="/tmp/myscript.lock" | |
if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null; | |
then | |
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT | |
## COMMAND TO RUN GOES HERE | |
# example: | |
sleep 60 | |
rm -f "$lockfile" | |
trap - INT TERM EXIT | |
else | |
echo "Failed to acquire lockfile: $lockfile. Already running?" | |
echo "Held by $(cat $lockfile)" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment