Skip to content

Instantly share code, notes, and snippets.

@blaxter
Created October 17, 2014 11:00
Show Gist options
  • Save blaxter/39a7e1363a1a537fbc3c to your computer and use it in GitHub Desktop.
Save blaxter/39a7e1363a1a537fbc3c to your computer and use it in GitHub Desktop.
script with lock
lockfile=/var/tmp/mylock
if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null; then
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
# do stuff here
# clean up after yourself, and release your trap
rm -f "$lockfile"
trap - INT TERM EXIT
else
echo "Lock Exists: $lockfile owned by $(cat $lockfile)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment