Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!samsung!know!ladcgw.ladc.bull.com!melb.bull.oz.au!sjg From: sjg@melb.bull.oz.au (Simon J Gerraty) Newsgroups: comp.unix.shell Subject: Re: Creating a lock file in sh? Message-ID: <1991Apr26.025921.9612@melb.bull.oz.au> Date: 26 Apr 91 02:59:21 GMT References: <1991Apr17.201931.14536@odin.corp.sgi.com> Organization: Bull HN Information Systems Australia. Lines: 54 In <1991Apr17.201931.14536@odin.corp.sgi.com> rhartman@thestepchild.sgi.com (Robert Hartman) writes: >In article pablo@sgi.com (Pablo Sanchez) writes: >>Hey there, >> >> I apologize if this has been hashed over a bizzillion times but can >>someone please send me their Bourne-shell lock-file implementation. >>Thanks. >Aside from the fact that it isn't guaranteed over NFS, you can get some >measure of protection this way: > if [ ! -w $file.lock ] ; then > echo $user: $$ `date` > $file.lock > [stuff deleted] generally speaking I think you'd have far fewer problems using mkdir. Nearly? all file related lock mechanisms will fail if used by root. mkdir on the other hand is guaranteed to fail if the directory exists. Here is the lock section from my addusr script I hope you get the idea (this would also work over NFS): lock_pw() { if [ "$pw_lock" = "no" ]; then if [ ! -w $PASSWD ]; then echo "Sorry, must be able to write \"${PASSWD}\"" exit 1 fi if mkdir ${PW_LOCK}; then pw_lock=me else echo "Sorry, \"${PASSWD}\" is busy. Try again later" exit 2 fi fi } unlock_pw() { if [ "$pw_lock" = "me" ]; then rmdir ${PW_LOCK} pw_lock=no fi } -- Simon J. Gerraty #include