Created
November 9, 2011 06:28
-
-
Save kenaniah/1350609 to your computer and use it in GitHub Desktop.
Linux lockfile wrapper
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/bash | |
# Lock file script written to ensure only once process at a time holds a lock | |
# Automatically kills zombie processes past the threshold | |
# https://gist.github.com/1350609 | |
if [ $# -ne 3 ]; then | |
echo "Usage: `basename $0` <lockfile> <timeout> <command>"; | |
exit 2; | |
fi | |
oldpid=0 | |
if [ -f $1.lock ]; then | |
oldpid=$(cat $1.lock) | |
fi | |
lockfile -r 1 -l $2 -s 1 $1.lock | |
[ $? -ne 0 ] && exit 1 | |
if [ $oldpid -ne 0 ]; then | |
kill -9 $oldpid | |
fi | |
chmod 644 $1.lock | |
echo $$ > $1.lock | |
chmod 444 $1.lock | |
eval $3 | |
rm -f $1.lock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment