pidor/scripts/semaphore.sh

26 lines
512 B
Bash
Raw Normal View History

2014-10-01 16:31:55 +02:00
#!/bin/bash
LockDir="/run/$(basename "$0").run"
P() {
while ! mkdir "$LockDir" 2>/dev/null
do
echo "cannot lock $LockDir, waiting"
LockDirStamp=$(stat -c %Y "$LockDir")
if [ "$LockDirStamp" != "" ] && [ "$LockDirStamp" -lt $(date --date "15 seconds ago" +%s) ]
then
echo lock is old, deleting
rmdir "$LockDir"
fi
sleep 1
done
echo got lock
}
V() {
echo "releasing $LockDir"
rmdir "$LockDir"
}
P
echo "running protected code"
sleep 10
V
echo "end protected code"