1#! /bin/sh 2# Shell-based mutex using mkdir. 3 4lockdir="$1" prog="$2"; shift 2 || exit 1 5 6# Remember when we started trying to acquire the lock. 7count=0 8touch lock-stamp.$$ 9 10trap 'rm -r "$lockdir" lock-stamp.$$' 0 11 12until 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 17 touch lock-stamp.$$ 18 count=1 19 # Steal the lock after 5 minutes. 20 elif [ $count = 300 ]; then 21 echo removing stale $lockdir >&2 22 rm -r "$lockdir" 23 else 24 echo waiting to acquire $lockdir >&2 25 fi 26 fi 27 sleep 1 28 count=`expr $count + 1` 29done 30 31echo $prog "$@" 32$prog "$@" 33 34# The trap runs on exit. 35