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.<br><br><br><div class="gmail_quote">On Fri, Jun 4, 2010 at 9:44 PM, JK <span dir="ltr"><<a href="mailto:jknapka@kneuro.net">jknapka@kneuro.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">I wrote:<br>
<br>
> You really must have a "create-file-only-if-it-doesn't-already-exist"<br>
> operation in order to make this work, and flock(1) is the first<br>
<br>
</div>To clarify: this is perfectly possible in a C program or whatever,<br>
but in a shellscript something like:<br>
<br>
echo $$ > /var/myname.pid<br>
<br>
is NOT atomic.<br>
<br>
Hmm, you could do:<br>
<br>
echo $$ >> /var/myname.pid<br>
<br>
and then check that the FIRST LINE contains your PID (via<br>
"head -n 1 /var/myname.pid").<br>
<font color="#888888"><br>
-- JK<br>
</font><div><div></div><div class="h5"><br>
<br>
On 6/4/2010 7:26 PM, JK wrote:<br>
><br>
><br>
> On 6/4/2010 7:16 PM, scott wrote:<br>
>> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512<br>
>><br>
>><br>
>> On Jun 4, 2010, at 8:49 PM, JK wrote:<br>
>><br>
>>> Doesn't anybody worry about race conditions any more?<br>
><br>
>><br>
>> when I write scripts that I dont want to run in parallel. I check for a /var/run/*.pid file<br>
>> related to the script and if the PID in it isnt mine, then I abort out. if the file isnt there, I<br>
>> create it with my PID. And when the script exits it removes the file.<br>
><br>
><br>
> You would appear to have a race condition if two instances<br>
> are started approximately simultaneously:<br>
><br>
> script 1:<br>
> check for /var/myname.pid - not found<br>
><br>
> CONTEXT SWITCH<br>
><br>
> script 2:<br>
> check for /var/myname.pid - not found<br>
><br>
> CONTEXT SWITCH<br>
><br>
> script 1:<br>
> create /var/myname.pid<br>
> write my pid<br>
><br>
> CONTEXT SWITCH<br>
><br>
> script 2:<br>
> create /var/myname.pid<br>
> write my pid<br>
><br>
> Script 1 is now happily running with the wrong pid in /var/myname.pid.<br>
> You really must have a "create-file-only-if-it-doesn't-already-exist"<br>
> operation in order to make this work, and flock(1) is the first<br>
> obvious thing I found that will do it. I thought "touch" might have<br>
> some magic to do the trick, but no. (Disappointingly.)<br>
><br>
> -- JK<br>
><br>
><br>
<br>
<br>
--<br>
Forget Jesus: stars died so that you could be here today.<br>
- physicist Lawrence Krauss<br>
_______________________________________________<br>
Ale mailing list<br>
<a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
<a href="http://mail.ale.org/mailman/listinfo/ale" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
See JOBS, ANNOUNCE and SCHOOLS lists at<br>
<a href="http://mail.ale.org/mailman/listinfo" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>-- <br>James P. Kinney III<br>Actively in pursuit of Life, Liberty and Happiness <br>Doing pretty well on all 3 pursuits <br><br>