[ale] Only one process

Jim Kinney jim.kinney at gmail.com
Sat Jun 5 09:06:20 EDT 2010


Since lsof will show a file open for execution before a write of the pid is
committed, use a grep on lsof for the count of opened copies. If greater
than one, exit.


On Fri, Jun 4, 2010 at 9:44 PM, JK <jknapka at kneuro.net> wrote:

> I wrote:
>
>  > 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
>
> To clarify: this is perfectly possible in a C program or whatever,
> but in a shellscript something like:
>
>   echo $$ > /var/myname.pid
>
> is NOT atomic.
>
> Hmm, you could do:
>
>   echo $$ >> /var/myname.pid
>
> and then check that the FIRST LINE contains your PID (via
> "head -n 1 /var/myname.pid").
>
> -- JK
>
>
> On 6/4/2010 7:26 PM, 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:
> >
> > script 1:
> >     check for /var/myname.pid - not found
> >
> > CONTEXT SWITCH
> >
> > script 2:
> >     check for /var/myname.pid - not found
> >
> > CONTEXT SWITCH
> >
> > script 1:
> >     create /var/myname.pid
> >     write my pid
> >
> > CONTEXT SWITCH
> >
> > script 2:
> >     create /var/myname.pid
> >     write my pid
> >
> > 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.)
> >
> > -- JK
> >
> >
>
>
> --
> Forget Jesus: stars died so that you could be here today.
>  - physicist Lawrence Krauss
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>



-- 
-- 
James P. Kinney III
Actively in pursuit of Life, Liberty and Happiness
Doing pretty well on all 3 pursuits
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.ale.org/pipermail/ale/attachments/20100605/527532bf/attachment-0001.html 


More information about the Ale mailing list