Hi, I&#39;m playing with more bash script related stuff. What I want is to build a library function that can be imported into many scripts and used to check that there isn&#39;t already an instance of that script running. <div>
<br></div><div>Often, the easy way of checking for duplicates is with a lock file. A file that is empty but its existence can be easily tested as a marker. Then when the process finishes the file can be deleted. If the process starts and the file already exists the process can exit knowing it is locked.</div>
<div><br></div><div>However, this requires the initial test and then careful handling of the lock file especially in the case of the script failing or not completing normally and therefore managing/removing the lock file.</div>
<div><br></div><div>So I figured I&#39;d create a simple function that checks for instances of $(basename 0) using ps. counts them and if the count is more than 1 then exit out the script logging an error. Things aren&#39;t that simple! When I run the script in one instance it produces one count and in another instance 1 gets added to the count without anything visible changing. Also this has not behaved consistantly claiming that another instance was running when it wasn&#39;t.</div>
<div><br></div><div>Here are the two script files.</div><div><br></div><div>controlLib.sh</div><div>=========</div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">function checkForDuplicateScript(){</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  SCRIPTNAME=$(basename $0)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  #declare INSTANCECOUNT as integer                                                      </font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  declare -i INSTANCECOUNT</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  INSTANCECOUNT=$(ps aux | grep &quot;$SCRIPTNAME&quot; | grep -v &quot;grep&quot; | wc -l 2&gt;/dev/null)</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  if [ &quot;$INSTANCECOUNT&quot; -gt &quot;2&quot; ]</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  then</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    echo &quot;Extra instance found of script: $SCRIPTNAME : Full ps output: &quot;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    ps aux | grep &quot;$SCRIPTNAME&quot;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    echo &quot;Instance Count: $INSTANCECOUNT : $BASHPID : $SCRIPTNAME&quot;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    exit 1</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">  fi</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">}</font></div><div><br></div></div><div><br></div><div>
test.sh</div><div>=====</div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">#!/bin/bash                                                                              </font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">####################################                                                     </font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"># Import Lib Functions                                                                   </font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">####################################                                                     </font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">. &quot;controlLib.sh&quot;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">echo &quot;Count 1: $(basename $0)&quot;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">ps aux | grep $(basename $0) | grep -v &quot;grep&quot; | wc -l</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">#Call lib function to check for duplicate instances of this script                      </font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">checkForDuplicateScript</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">sleep 10</font></div></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">exit 0</font></div><div><br></div><div>output from instance 1</div><div>=================</div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Count 1: test.sh</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">1</font></div></div><div><br></div><div>output from instane 2</div><div>================</div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Count 1: test.sh</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">2</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Extra instance found of script: test.sh : Full ps output: </font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">amon      4856  2.0  0.0  12316  1656 pts/0    S+   21:11   0:00 /bin/bash ./test.sh</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">amon      4870  2.0  0.0  12312  1660 pts/1    S+   21:11   0:00 /bin/bash ./test.sh</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">amon      4884  0.0  0.0   9140  1020 pts/1    S+   21:11   0:00 grep test.sh</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Instance Count: 3 : 4870 : test.sh</font></div>
</div><div><br></div><div><br></div><div>For some reason the count is being incremented yet ps lists the expected output.</div><div>Note that when ps runs to count it uses &quot;grep -v grep&quot; so that it should only count actual instances and not the grep command. When the ps produces output to the screen it doesn&#39;t do that just in case something else is a potential match.</div>
<div><br></div><div>The main question is Why when I assign it to a variable is <span class="Apple-style-span" style="font-family:&#39;courier new&#39;,monospace">INSTANCECOUNT</span> being incremented by one (giving 3 rather than 2)?</div>
<div><br></div><div>My second question, does anyone have any other suggestions on managing this one? I have worked with lock files before, but they require more management within a script than I&#39;d prefer to have to keep an eye on. I want to achieve the goal of one clean and simple line command at the start of a script that does the check, manages the conflict logging (and or emailing) an error message etc...</div>
<div><br></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8">Any thoughts from people on this list?</div><meta http-equiv="content-type" content="text/html; charset=utf-8">