[ale] Only one process

Chris Fowler cfowler at outpostsentinel.com
Fri Jun 4 21:41:26 EDT 2010


On Fri, 2010-06-04 at 19:26 -0600, JK wrote:
> 
> On 6/4/2010 7:16 PM, scott wrote:
> > -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512
> >
> >
> > On Jun 4, 2010, at 8:49 PM, JK wrote:
> >
> >> Doesn't anybody worry about race conditions any more?
> 
> >
> > when I write scripts that I dont want to run in parallel.  I check for a /var/run/*.pid file
> > related to the script and if the PID in it isnt mine, then I abort out. if the file isnt there, I
> > create it with my PID.  And when the script exits it removes the file.
> 
> 
> You would appear to have a race condition if two instances
> are started approximately simultaneously:
> 

good catch.


> 
> Script 1 is now happily running with the wrong pid in /var/myname.pid.
> You really must have a "create-file-only-if-it-doesn't-already-exist"
> operation in order to make this work, and flock(1) is the first
> obvious thing I found that will do it.  I thought "touch" might have
> some magic to do the trick, but no. (Disappointingly.)
> 

       flock()  places  advisory  locks  only; given suitable
permissions on a
       file, a process is free to ignore the use of flock() and perform
I/O on
       the file.


If both scripts use flock then it should be okay.  I really like using
semaphores for critical areas but I've not have huge success with them
under Perl like I've had in C.  Plus, in this case a semaphore would be
a bit of overkill.

I've still got some more work to do on the script to try and "guarantee"
some level of success.  This script's purpose is to be called when PRI
or T1 trunks go down and it uses Expect heavily to coerce the link into
coming back up.  If that fails it generates a trouble ticket.  So far it
works nicely but there are some places where I'm exiting due to failure
and I need to create a row in the database to create the ticket right
before I exit.  If the script has any issues it will abandon the task
and let a person take over but that person does need to be notified.





More information about the Ale mailing list