#! /bin/sh # # example using good lock (mkdir and directory, atomic) lockflag=0 lock() { local n=5 while [ "$n" -gt 0 ]; do echo "$$ locking" mkdir lockdir 2>/dev/null if [ "$?" = 0 ]; then lockflag=1; break; fi # got the lock n=$((n-1)) sleep 1 done } unlock() { echo "$$ unlocking" rmdir lockdir 2>/dev/null lockflag=0 } lock if [ "$lockflag" = 1 ]; then echo "$$ got lock, working..." # do some work here... sleep 3 unlock else echo "$$ failed" fi