Created
March 4, 2013 07:00
-
-
Save bradclawsie/5080527 to your computer and use it in GitHub Desktop.
a wrapper to handle graceful lock breaking (when needed) of golock programs
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/zsh | |
# call like: | |
# $ run-exclusive.sh /path/of/progname /path/of/lockfile | |
# e.g. | |
# $ run-exclusive.sh ./testlock t.lock | |
# clearly you want progname to be something that will only match once. | |
# don't use a short string that is a substring match of something else running | |
export PROGNAME=$1 | |
# GOLOCKNAME is an env var that is hardcoded into the golock package. the lock | |
# file name MUST be assigned to an env var with this name. when running | |
# correctly, $PROGNAME will open a file called $GOLOCKNAME and get | |
# an exclusive lock on it. | |
export GOLOCKNAME=$2 | |
echo "running: $PROGNAME with golock lock file $GOLOCKNAME" | |
# if the lockfile exists, break it (rm -f) if there is no other | |
# copy of $PROGNAM running or if there is a copy of it running as a zombie | |
if [ -e $GOLOCKNAME ]; then | |
echo "lockfile $GOLOCKNAME found" | |
MATCHRUN=`ps -e -o pid,stat,cmd | grep -v grep | grep $PROGNAME | cut -d" " -f3 -` | |
NUMMATCHES=`echo $MATCHRUN | wc -l` | |
BROKELOCK="no" | |
if [ $NUMMATCHES '==' "1" ]; then | |
# *this* script was the only match | |
echo "no other $PROGNAME running..." | |
rm -f $GOLOCKNAME | |
BROKELOCK="yes" | |
else | |
for i in $MATCHRUN; do | |
if [ $i '==' "Z" ]; then | |
echo "zombie found..." | |
rm -f $GOLOCKNAME | |
BROKELOCK="yes" | |
break | |
fi | |
done | |
fi | |
# rm -f wouldn't remove the lock file if the permissions were wrong | |
if [ $BROKELOCK '==' "yes" ]; then | |
if [ -e $GOLOCKNAME ]; then | |
echo "did not break the lock, see user/group ownership of $GOLOCKNAME" | |
else | |
echo "broke the lock" | |
fi | |
else | |
echo "lock appears legit" | |
fi | |
fi | |
exec $PROGNAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment