Last active
August 29, 2015 14:27
-
-
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
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 | |
# 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