Skip to content

Instantly share code, notes, and snippets.

@kenaniah
Created November 9, 2011 06:28
Show Gist options
  • Save kenaniah/1350609 to your computer and use it in GitHub Desktop.
Save kenaniah/1350609 to your computer and use it in GitHub Desktop.
Linux lockfile wrapper
#!/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