Skip to content

Instantly share code, notes, and snippets.

@MagerValp
Last active August 29, 2015 14:27
Show Gist options
  • Save MagerValp/5f3829fb46f5f63c26cd to your computer and use it in GitHub Desktop.
Save MagerValp/5f3829fb46f5f63c26cd to your computer and use it in GitHub Desktop.
Shell script that uses an flock on itself to avoid multiple instances being run
#!/bin/bash
# Based on https://plus.google.com/+RandalLSchwartz/posts/QcrqvT3mUdy
exec 200< "$0"
if ! perl -MFcntl=LOCK_EX,LOCK_NB -e 'open(FH, "<&=200"); flock(FH, LOCK_EX|LOCK_NB) or exit 1'; then
echo "There can be only one!"
exit 1
fi
echo "Locked and safe"
sleep 10
echo "Unlocking and exiting"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment