Hi, I'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'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'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'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'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="'courier new', monospace">function checkForDuplicateScript(){</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> SCRIPTNAME=$(basename $0)</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> #declare INSTANCECOUNT as integer </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> declare -i INSTANCECOUNT</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> INSTANCECOUNT=$(ps aux | grep "$SCRIPTNAME" | grep -v "grep" | wc -l 2>/dev/null)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> if [ "$INSTANCECOUNT" -gt "2" ]</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> then</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> echo "Extra instance found of script: $SCRIPTNAME : Full ps output: "</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> ps aux | grep "$SCRIPTNAME"</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> echo "Instance Count: $INSTANCECOUNT : $BASHPID : $SCRIPTNAME"</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> exit 1</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> fi</font></div><div><font class="Apple-style-span" face="'courier new', monospace">}</font></div><div><br></div></div><div><br></div><div>
test.sh</div><div>=====</div><div><div><font class="Apple-style-span" face="'courier new', monospace">#!/bin/bash </font></div><div><font class="Apple-style-span" face="'courier new', monospace">#################################### </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"># Import Lib Functions </font></div><div><font class="Apple-style-span" face="'courier new', monospace">#################################### </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">. "controlLib.sh"</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">echo "Count 1: $(basename $0)"</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">ps aux | grep $(basename $0) | grep -v "grep" | wc -l</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">#Call lib function to check for duplicate instances of this script </font></div><div><font class="Apple-style-span" face="'courier new', monospace">checkForDuplicateScript</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">sleep 10</font></div></div><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">exit 0</font></div><div><br></div><div>output from instance 1</div><div>=================</div><div><div><font class="Apple-style-span" face="'courier new', monospace">Count 1: test.sh</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">1</font></div></div><div><br></div><div>output from instane 2</div><div>================</div><div><div><font class="Apple-style-span" face="'courier new', monospace">Count 1: test.sh</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">2</font></div><div><font class="Apple-style-span" face="'courier new', monospace">Extra instance found of script: test.sh : Full ps output: </font></div>
<div><font class="Apple-style-span" face="'courier new', 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="'courier new', 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="'courier new', 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="'courier new', 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 "grep -v grep" so that it should only count actual instances and not the grep command. When the ps produces output to the screen it doesn'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:'courier new',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'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">