mono-infrastructure/ansible/playbooks/files/usr/local/bin/lock-wrapper

52 lines
950 B
Plaintext
Raw Permalink Normal View History

2020-12-31 21:49:48 +00:00
#!/bin/bash
# Borrowed from Fedora Infra for Rocky Linux
if [ $# -lt 2 ]; then
echo "Usage: $0 [name] [script]"
exit 1;
fi
NAME=$1
SCRIPT=$2
SILENT="no"
if [ $# -ge 3 -a "$3" == "--silent" ]; then
SILENT="yes"
shift
fi
shift 2
LOCKDIR="/var/tmp/$NAME"
PIDFILE="$LOCKDIR/pid"
function cleanup {
rm -rf "$LOCKDIR"
}
RESTORE_UMASK=$(umask -p)
umask 0077
mkdir "$LOCKDIR" >& /dev/null
if [ $? != 0 ]; then
PID=$(cat "$PIDFILE")
if [ -n "$PID" ] && /bin/ps $PID > /dev/null
then
if [ "$SILENT" != "yes" ]; then
2021-01-01 03:05:27 +00:00
echo "$PID is still running"
/bin/ps -o user,pid,start,time,comm $PID
fi
2020-12-31 21:49:48 +00:00
exit 1;
else
echo "$LOCKDIR exists but $PID is dead"
echo "Removing lockdir and re-running"
/bin/rm -rf $LOCKDIR
mkdir $LOCKDIR || exit
fi
fi
trap cleanup EXIT SIGQUIT SIGHUP SIGTERM
echo $$ > "$PIDFILE"
$RESTORE_UMASK
eval "$SCRIPT $*"