Skip to content

Instantly share code, notes, and snippets.

@eripa
Created March 18, 2016 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eripa/0a8a4892a2a2a6f6f508 to your computer and use it in GitHub Desktop.
Save eripa/0a8a4892a2a2a6f6f508 to your computer and use it in GitHub Desktop.
shell wrapper lockfile
#!/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