<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How Can I Tell If My Bash Script Is Already Running?</title>
	<atom:link href="http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/</link>
	<description>thoughts of a web geek</description>
	<lastBuildDate>Tue, 09 Mar 2010 08:53:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Peter-Alexander</title>
		<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/comment-page-1/#comment-4818</link>
		<dc:creator>Peter-Alexander</dc:creator>
		<pubDate>Tue, 09 Mar 2010 08:53:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/#comment-4818</guid>
		<description>I tried to achieve the same and had the ps command in mind. While struggling with syntax I began searching the web and gladly found this site. Thank you all for posting here. The solution from Dominic in post 13 works perfect for me - I could just copy&amp;paste it.

I must admit the flock command looks very nice too but requires to use it when calling the script.
Peter</description>
		<content:encoded><![CDATA[<p>I tried to achieve the same and had the ps command in mind. While struggling with syntax I began searching the web and gladly found this site. Thank you all for posting here. The solution from Dominic in post 13 works perfect for me &#8211; I could just copy&amp;paste it.</p>
<p>I must admit the flock command looks very nice too but requires to use it when calling the script.<br />
Peter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Standard Mischief &#187; common bash misconception</title>
		<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/comment-page-1/#comment-4417</link>
		<dc:creator>Standard Mischief &#187; common bash misconception</dc:creator>
		<pubDate>Wed, 25 Mar 2009 10:35:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/#comment-4417</guid>
		<description>[...] common misconception here is the idea that the bash built-in variable &#8220;$$&#8221; holds the PID (process ID) of the script itself. That is not true. It&#8217;s [...]</description>
		<content:encoded><![CDATA[<p>[...] common misconception here is the idea that the bash built-in variable &#8220;$$&#8221; holds the PID (process ID) of the script itself. That is not true. It&#8217;s [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Franzone</title>
		<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/comment-page-1/#comment-4164</link>
		<dc:creator>Jonathan Franzone</dc:creator>
		<pubDate>Mon, 27 Oct 2008 20:11:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/#comment-4164</guid>
		<description>Sorry about the less than / greater than signs, guys! :( This is plain vanilla install of WordPress without many plugins or mods.</description>
		<content:encoded><![CDATA[<p>Sorry about the less than / greater than signs, guys! <img src='http://www.franzone.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  This is plain vanilla install of WordPress without many plugins or mods.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dominic</title>
		<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/comment-page-1/#comment-4162</link>
		<dc:creator>Dominic</dc:creator>
		<pubDate>Mon, 27 Oct 2008 11:43:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/#comment-4162</guid>
		<description>Yet again I am thrown by the way this website handles &gt; and &lt;, you have to enter them with ampersand gt;, and ampersand lt;, backslashes don&#039;t work! So my text above should have read:Thanks for your input Mike.

As you guessed, if a script has been stopped (e.g. with kill) then it seems to hang around a bit in the ps listing and comm column reports the scriptname followed by space and &lt;defunct&gt; (at least it does on mine). But according to â€˜man psâ€™ the Z status in comm column does the same thing, so yours should work too (but I havenâ€™t tested it). This is particularly likely to happen if you are working on the script, killing one instance and wanting to start another. The script lets you do this without having to wait for the first killed instance to disappear.</description>
		<content:encoded><![CDATA[<p>Yet again I am thrown by the way this website handles &gt; and &lt;, you have to enter them with ampersand gt;, and ampersand lt;, backslashes don&#8217;t work! So my text above should have read:Thanks for your input Mike.</p>
<p>As you guessed, if a script has been stopped (e.g. with kill) then it seems to hang around a bit in the ps listing and comm column reports the scriptname followed by space and &lt;defunct&gt; (at least it does on mine). But according to â€˜man psâ€™ the Z status in comm column does the same thing, so yours should work too (but I havenâ€™t tested it). This is particularly likely to happen if you are working on the script, killing one instance and wanting to start another. The script lets you do this without having to wait for the first killed instance to disappear.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dominic</title>
		<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/comment-page-1/#comment-4161</link>
		<dc:creator>Dominic</dc:creator>
		<pubDate>Mon, 27 Oct 2008 11:40:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/#comment-4161</guid>
		<description>Thanks for your input Mike.

As you guessed, if a script has been stopped (e.g. with kill) then it seems to hang around a bit in the ps listing and comm column reports the scriptname followed by space and \ (at least it does on mine). But according to &#039;man ps&#039; the Z status in comm column does the same thing, so yours should work too (but I haven&#039;t tested it). This is particularly likely to happen if you are working on the script, killing one instance and wanting to start another. The script lets you do this without having to wait for the first killed instance to disappear.</description>
		<content:encoded><![CDATA[<p>Thanks for your input Mike.</p>
<p>As you guessed, if a script has been stopped (e.g. with kill) then it seems to hang around a bit in the ps listing and comm column reports the scriptname followed by space and \ (at least it does on mine). But according to &#8216;man ps&#8217; the Z status in comm column does the same thing, so yours should work too (but I haven&#8217;t tested it). This is particularly likely to happen if you are working on the script, killing one instance and wanting to start another. The script lets you do this without having to wait for the first killed instance to disappear.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Kennedy</title>
		<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/comment-page-1/#comment-4155</link>
		<dc:creator>Mike Kennedy</dc:creator>
		<pubDate>Sat, 25 Oct 2008 21:46:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/#comment-4155</guid>
		<description>Thanks, Dominic. That&#039;s much better!
My basic problem is that I&#039;ve not seen &quot;defunct/zombie&quot; processes anyway, so I&#039;m kinda &quot;shooting in the dark&quot; here. If the &quot;comment&quot; on zombies processes has both &quot;defunct&quot; and the process name, then the PS will extract the matches on the script name, and GREP#2 will also work fine.
With my &quot;fix&quot;, I need the &quot;comment&quot; column, just to ensure there&#039;s a space after the &quot;Z&quot; status. Also, it might be comforting to see the actual name on the extracted list, if one ever goes digging...
Finally, I think your approach will ALWAYS catch dupes, even in extreme timing conditions - eg, process#2 started N nono-secs after process#1, etc, etc, and will never get into any &quot;race conditions&quot;, etc.
Thank you for a very very nice solution.
  - Mike</description>
		<content:encoded><![CDATA[<p>Thanks, Dominic. That&#8217;s much better!<br />
My basic problem is that I&#8217;ve not seen &#8220;defunct/zombie&#8221; processes anyway, so I&#8217;m kinda &#8220;shooting in the dark&#8221; here. If the &#8220;comment&#8221; on zombies processes has both &#8220;defunct&#8221; and the process name, then the PS will extract the matches on the script name, and GREP#2 will also work fine.<br />
With my &#8220;fix&#8221;, I need the &#8220;comment&#8221; column, just to ensure there&#8217;s a space after the &#8220;Z&#8221; status. Also, it might be comforting to see the actual name on the extracted list, if one ever goes digging&#8230;<br />
Finally, I think your approach will ALWAYS catch dupes, even in extreme timing conditions &#8211; eg, process#2 started N nono-secs after process#1, etc, etc, and will never get into any &#8220;race conditions&#8221;, etc.<br />
Thank you for a very very nice solution.<br />
  &#8211; Mike</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dominic</title>
		<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/comment-page-1/#comment-4149</link>
		<dc:creator>Dominic</dc:creator>
		<pubDate>Sat, 25 Oct 2008 15:35:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/#comment-4149</guid>
		<description>Hi all

Sorry for the confusion. My code was right as I wrote it in posting 9 but it came out wrong on the web page because it involves &#039;greater than&#039; and &#039;less than&#039; codes on either side of the word &#039;defunct&#039; after the first inverted comma after the last grep (er got that?). I&#039;m not sure how to get it to come out correct on this page, but I&#039;ll try below. Anyway seems like Mike&#039;s approach works as good as mine, the &#039;Z&#039; in stat column is same (I think) as &#039;defunct&#039; in comm column. Maybe in Mike&#039;s the comm column is not needed at all?

This is the solution I intended to show in comment 9, hope it comes out right this time:

if [ ! -z &quot;`ps -C \`basename $0\` --no-headers -o &quot;pid,ppid,sid,comm&quot;&#124;grep -v &quot;$$ &quot;&#124;grep -v &quot;&lt;defunct&gt;&quot;`&quot; ]; then
#script is already running - abort
exit 1
fi</description>
		<content:encoded><![CDATA[<p>Hi all</p>
<p>Sorry for the confusion. My code was right as I wrote it in posting 9 but it came out wrong on the web page because it involves &#8216;greater than&#8217; and &#8216;less than&#8217; codes on either side of the word &#8216;defunct&#8217; after the first inverted comma after the last grep (er got that?). I&#8217;m not sure how to get it to come out correct on this page, but I&#8217;ll try below. Anyway seems like Mike&#8217;s approach works as good as mine, the &#8216;Z&#8217; in stat column is same (I think) as &#8216;defunct&#8217; in comm column. Maybe in Mike&#8217;s the comm column is not needed at all?</p>
<p>This is the solution I intended to show in comment 9, hope it comes out right this time:</p>
<p>if [ ! -z "`ps -C \`basename $0\` --no-headers -o "pid,ppid,sid,comm"|grep -v "$$ "|grep -v "&lt;defunct&gt;"`" ]; then<br />
#script is already running &#8211; abort<br />
exit 1<br />
fi</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Soren</title>
		<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/comment-page-1/#comment-4144</link>
		<dc:creator>Soren</dc:creator>
		<pubDate>Fri, 24 Oct 2008 14:45:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/#comment-4144</guid>
		<description>Hi, crazy to see this thread still alive after more than a year,

If it is not possible, for some reason, to use some of the improved alternatives proposed, I will like to defend my original locking in comment 1. Michael Kennedy suggests a possible race condition in comment 3, but I don&#039;t think that is possible, since the process number is being reread in the LOCK=`cat â€œ${LCK_FILE}â€` statement after it has been write protected. If a possible racing second script would override the process id in the file before the read only line, then there would be a mismatch in the following if after the id was read again from the file.

Anyway I would probably in most cases go for the flock method, the ps hack or similar more elegant solutions.

Soren</description>
		<content:encoded><![CDATA[<p>Hi, crazy to see this thread still alive after more than a year,</p>
<p>If it is not possible, for some reason, to use some of the improved alternatives proposed, I will like to defend my original locking in comment 1. Michael Kennedy suggests a possible race condition in comment 3, but I don&#8217;t think that is possible, since the process number is being reread in the LOCK=`cat â€œ${LCK_FILE}â€` statement after it has been write protected. If a possible racing second script would override the process id in the file before the read only line, then there would be a mismatch in the following if after the id was read again from the file.</p>
<p>Anyway I would probably in most cases go for the flock method, the ps hack or similar more elegant solutions.</p>
<p>Soren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Kennedy</title>
		<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/comment-page-1/#comment-4143</link>
		<dc:creator>Mike Kennedy</dc:creator>
		<pubDate>Fri, 24 Oct 2008 14:08:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/#comment-4143</guid>
		<description>Folks,
I had a look at &quot;defunct/zombie&quot; processes, and maybe the following tweaks would do the trick:

if [ ! -z &quot;`ps -C \`basename $0\` --no-headers -o &quot;pid,ppid,sid,stat,comm&quot;&#124;grep -v &quot;$$ &quot;&#124;grep -v &quot; Z &quot;`&quot; ]; then
#script is already running - abort
exit 1
fi

This includes the &quot;stat&quot; column in the data going from PS to GREP-1. A &quot;Z&quot; in that field indicates a Zombie process, and these are then excluded from the list. Anything left = &quot;Dupe Launch&quot;.

With the space-Z-space test on the 2nd GREP, and if the BASH script happened to be named just &quot;Z&quot;, then, can someone confirm:
- a running instance would appear as &quot;nnn nnn nnn R Z&quot;, and the 2nd GREP would not find a space after the &quot;Z&quot;, and would not purge that entry from the list, correctly indicating &quot;Dupe&quot;.
- a defunct instance would show as &quot;nnn nnn nnn Z Z&quot;. The 2nd GREP would match on the space-Z-space, clear it from the list, correctly indicating &quot;Not a Dupe&quot;.
Hopefully!!
--Mike



-- Mike</description>
		<content:encoded><![CDATA[<p>Folks,<br />
I had a look at &#8220;defunct/zombie&#8221; processes, and maybe the following tweaks would do the trick:</p>
<p>if [ ! -z "`ps -C \`basename $0\` --no-headers -o "pid,ppid,sid,stat,comm"|grep -v "$$ "|grep -v " Z "`" ]; then<br />
#script is already running &#8211; abort<br />
exit 1<br />
fi</p>
<p>This includes the &#8220;stat&#8221; column in the data going from PS to GREP-1. A &#8220;Z&#8221; in that field indicates a Zombie process, and these are then excluded from the list. Anything left = &#8220;Dupe Launch&#8221;.</p>
<p>With the space-Z-space test on the 2nd GREP, and if the BASH script happened to be named just &#8220;Z&#8221;, then, can someone confirm:<br />
- a running instance would appear as &#8220;nnn nnn nnn R Z&#8221;, and the 2nd GREP would not find a space after the &#8220;Z&#8221;, and would not purge that entry from the list, correctly indicating &#8220;Dupe&#8221;.<br />
- a defunct instance would show as &#8220;nnn nnn nnn Z Z&#8221;. The 2nd GREP would match on the space-Z-space, clear it from the list, correctly indicating &#8220;Not a Dupe&#8221;.<br />
Hopefully!!<br />
&#8211;Mike</p>
<p>&#8211; Mike</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Kennedy</title>
		<link>http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/comment-page-1/#comment-4140</link>
		<dc:creator>Mike Kennedy</dc:creator>
		<pubDate>Thu, 23 Oct 2008 22:08:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.franzone.com/2007/09/23/how-can-i-tell-if-my-bash-script-is-already-running/#comment-4140</guid>
		<description>Jonathan, Dominic,

[It&#039;s over a year since I posted here, so one is overdue!!].

Re Dominic&#039;s most recent version, I tried it, and had problems with the &quot;Defunct&quot; check (which I don&#039;t understand anyway!). When the 2nd instance starts up:
  - the PS command correctly extracted the pid/ppid/etc of the 2 x instances.
  - the GREP -v &quot;$$ &quot; operation on PID correctly discarded the 2nd instance.
  - the defunct GREP -v &quot;&quot; operation discarded the 1st instance line, leaving an empty response, and thereby the overall duplicate instance was not detected.

Maybe, if I understood the &quot;defunct&quot; issues, I could help further!

Best regards,
  - Mike</description>
		<content:encoded><![CDATA[<p>Jonathan, Dominic,</p>
<p>[It's over a year since I posted here, so one is overdue!!].</p>
<p>Re Dominic&#8217;s most recent version, I tried it, and had problems with the &#8220;Defunct&#8221; check (which I don&#8217;t understand anyway!). When the 2nd instance starts up:<br />
  &#8211; the PS command correctly extracted the pid/ppid/etc of the 2 x instances.<br />
  &#8211; the GREP -v &#8220;$$ &#8221; operation on PID correctly discarded the 2nd instance.<br />
  &#8211; the defunct GREP -v &#8220;&#8221; operation discarded the 1st instance line, leaving an empty response, and thereby the overall duplicate instance was not detected.</p>
<p>Maybe, if I understood the &#8220;defunct&#8221; issues, I could help further!</p>
<p>Best regards,<br />
  &#8211; Mike</p>
]]></content:encoded>
	</item>
</channel>
</rss>
