On Tue, 6 Mar 2007, Dennis Peterson wrote:

> From: Dennis Peterson <[EMAIL PROTECTED]>
> To: ClamAV users ML <clamav-users@lists.clamav.net>
> Date: Tue, 06 Mar 2007 11:18:30 -0800
> Subject: Re: [Clamav-users] msrbl sigs: rsync
> Reply-To: ClamAV users ML <clamav-users@lists.clamav.net>

...

> if [ -f "$RunFlag" ]; then
>   echo "This script already running. Cleaning up..."
>   /usr/bin/rm $RunFlag
>   /usr/bin/pkill sanesecurity.sh
> else
>   /usr/bin/touch $RunFlag
> fi

...
This is getting a bit off-topic, but here goes anyway...

I believe there are potential race problems in the above method
of locking.  Not necessarily in this case where the script is run
infrequently.  But in general when you've no idea when a particular
script may run.

Appended below is the shell script fragment I use in scripts that
require locking.  You'll need to adjust command definitions etc to
taste.

This fragment uses the fact that hard-linking is atomic on (all?)
Unix systems.  So it creates a temporary file with a unique name
and attempts to hard-link the lockname to it.  If the hard-linking
succeeds, you've definitely got the lock.  If not, it's already in
use.  As window dressing, the script will try five times to get the
lock and waits for a minute between retries.

I'm not going to claim originality for this fragment.  I'm sure I
developed it from a similar fragment I saw in the Usenet C News
scripts[1].

Here's the fragment:


sleeptime=60  # one minute.
max=5        # max no of attempts to get a lock.

echo=/bin/echo
ln=/bin/ln
rm=/bin/rm
sleep=/bin/sleep

...

cd {some directory} || exit 1

# Avoid straying into the gunsights of some other brave soldier...
pid=$$
lockname=LOCK_FETCH
lock_temp=$lockname.$pid
trap "$rm -f $lock_temp; trap 0" 0 1 2 15
$echo $pid > $lock_temp || exit 1

tries=
while :
do
  if $ln $lock_temp $lockname 2> /dev/null
  then
    $rm -f $lock_temp
    trap "$rm -f $lockname; trap 0" 0 1 2 15
    break
  fi
  tries=$(($tries + 1))
  [ $tries -gt $max ] && exit 1
  $sleep $sleeptime
done


[1] "Managing Usenet", Henry Spencer & David Lawrence, O'Reilley &
    Associates Inc, 1998, ISBN 1-56592-198-4
-- 
Dennis Davis, BUCS, University of Bath, Bath, BA2 7AY, UK
[EMAIL PROTECTED]               Phone: +44 1225 386101
_______________________________________________
Help us build a comprehensive ClamAV guide: visit http://wiki.clamav.net
http://lurker.clamav.net/list/clamav-users.html

Reply via email to