Lock Files From Shell Scripts

David Goodenough dg at lakart.UUCP
Thu Nov 9 05:23:57 AEST 1989


mercer at ncrcce.StPaul.NCR.COM (Dan Mercer) sez:
> ..... Then I considered using ln,  thinking an error
> condition would be returned if the file existed.  But no,  the file
> was overwritten unconditionally.

Humm - sounds like SYSV and a funky ln. However, if mkdir will overwrite
an already existing directory then I'd say you have a really badly broken
system. So why not:

--- cut here --- cut here --- cut here --- cut here --- cut here ---
lock=/tmp/lock

until mkdir $lock 2>&1 >/dev/null
do
    sleep 10
done

# critical stuff

# clean up the lock directory
rmdir $lock

exit 0
--- cut here --- cut here --- cut here --- cut here --- cut here ---

You could also use umask and touch, except touch doesn't return an error
status, at least BSD touch doesn't (again, if mkdir doesn't you're in
dead trouble anyway)
-- 
	dg at lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at xait.xerox.com			  +---+



More information about the Comp.unix.questions mailing list