limosek-zaf/lib/zaflock

34 lines
650 B
Plaintext
Raw Normal View History

2016-03-24 15:46:42 +01:00
#!/bin/sh
. $(dirname $0)/preload.sh
2016-03-24 15:46:42 +01:00
help() {
zaf_err "$0 key cmd"
2016-03-24 15:46:42 +01:00
}
lkey="$1"
[ -z "$lkey" ] && help
shift
[ -z "${ZAF_LOCK_SECONDS}" ] && seconds=5
[ -z "${ZAF_LOCK_FORCE}" ] && force=1
2016-04-21 15:29:48 +02:00
lockfile="/tmp/zaflock_${lkey}"
2016-03-24 15:46:42 +01:00
i=0
while [ -f "$lockfile" ] && [ $i -lt $seconds ]; do
sleep 1
i=$(expr $i + 1)
done
if [ -f "$lockfile" ] && [ -n "$force" ]; then
logger -s -t "zlock" -p daemon.warn "Releasing $lockfile!"
rm -f "$lockfile"
fi
if [ -f "$lockfile" ] && [ -z "$force" ]; then
logger -s -t "zlock" -p daemon.err "Could not get lock for $lockfile!"
exit 1
fi
2016-04-21 15:29:48 +02:00
[ -n "$*" ] && $@ 2>"$lockfile"
[ -s "$lockfile" ] && zaf_wrn <"$lockfile"
2016-03-24 15:46:42 +01:00