runlock
Table of contents
Shell script utility: runlock
This piece of code prevents multiple copies of the same program (i.e. crontab jobs) to run at the same time.
#!/bin/sh
if [ "\$1" != "1" ]; then
# Debug
#exit
fi
ECHO=/bin/echo
GREP=/usr/bin/grep
HOSTNAME=/bin/hostname
MAIL=/usr/bin/mail
PS=/bin/ps
RM=/bin/rm
TOUCH=/usr/bin/touch
if [ -z $MAILTO ]; then
MAILTO="user@domain"
fi
if [ -e /tmp/$1.lock ]; then
HOST=`$HOSTNAME`
LIST=`$PS axuwww|$GREP \$1`
LIST="\n\n$LIST\n"
$ECHO "Could not execute \$1 due to lockfile...$LIST" | $MAIL -s "LOCKFILE: $HOST - error running \$1" $MAILTO
exit 1;
fi
$TOUCH /tmp/\$1.lock
\$2
$RM /tmp/\$1.lock