[comp.unix.shell] Creating a lock file in sh?

pablo@csd.sgi.com (Pablo Sanchez) (04/17/91)

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.
--

Pablo Sanchez                   ``Why is it we never know exactly when we fall
Silicon Graphics, Inc             in love, but we always know when we fall
pablo@sgi.com                     out of love?''    --  L.A. Story

rhartman@thestepchild.sgi.com (Robert Hartman) (04/18/91)

In article <PABLO.91Apr17110430@tofu-hut.csd.sgi.com> 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
		if [ $$ -eq `awk '{print $2}' $file.lock` ] ; then
			# enter protected section
			# ...
			# exit protected section
		else
			echo "$file in use" ; exit 1
		fi
	else
		echo "$file in use" ; exit 1
	fi

-r

harrison@necssd.NEC.COM (Mark Harrison) (04/19/91)

In article <1991Apr17.201931.14536@odin.corp.sgi.com>,
rhartman@thestepchild.sgi.com (Robert Hartman) writes:

> Aside from the fact that it isn't guaranteed over NFS, you can get some
> measure of protection this way:

	[example using lockfile]

Could you elaborate on why it's not guaranteed over NFS?

Mark. ("Yikes!")
-- 
Mark Harrison             harrison@ssd.dl.nec.com
(214)518-5050             {necntc, cs.utexas.edu}!necssd!harrison
standard disclaimers apply...

sjg@melb.bull.oz.au (Simon J Gerraty) (04/26/91)

In <1991Apr17.201931.14536@odin.corp.sgi.com> rhartman@thestepchild.sgi.com (Robert Hartman) writes:
>In article <PABLO.91Apr17110430@tofu-hut.csd.sgi.com> 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		<sjg@melb.bull.oz.au>

#include <disclaimer,_witty_comment>