2 # Shell-based mutex using mkdir.
4 lockdir="$1" prog="$2"; shift 2 || exit 1
6 # Remember when we started trying to acquire the lock.
10 trap 'rm -r "$lockdir" lock-stamp.$$' 0
12 until mkdir "$lockdir" 2>/dev/null; do
13 # Say something periodically so the user knows what's up.
14 if [ `expr $count % 30` = 0 ]; then
15 # Reset if the lock has been renewed.
16 if [ -n "`find \"$lockdir\" -newer lock-stamp.$$`" ]; then
19 # Steal the lock after 5 minutes.
20 elif [ $count = 300 ]; then
21 echo removing stale $lockdir >&2
24 echo waiting to acquire $lockdir >&2
28 count=`expr $count + 1`
34 # The trap runs on exit.