[Klug-general] Checking For Duplicate Instance Of Script

David Halliday david.halliday at gmail.com
Tue Dec 20 21:32:48 UTC 2011


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.

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.

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.

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.

Here are the two script files.

controlLib.sh
=========
function checkForDuplicateScript(){
  SCRIPTNAME=$(basename $0)
  #declare INSTANCECOUNT as integer

  declare -i INSTANCECOUNT
  INSTANCECOUNT=$(ps aux | grep "$SCRIPTNAME" | grep -v "grep" | wc -l
2>/dev/null)
  if [ "$INSTANCECOUNT" -gt "2" ]
  then
    echo "Extra instance found of script: $SCRIPTNAME : Full ps output: "
    ps aux | grep "$SCRIPTNAME"
    echo "Instance Count: $INSTANCECOUNT : $BASHPID : $SCRIPTNAME"
    exit 1
  fi
}


test.sh
=====
#!/bin/bash

####################################

# Import Lib Functions

####################################

. "controlLib.sh"

echo "Count 1: $(basename $0)"
ps aux | grep $(basename $0) | grep -v "grep" | wc -l

#Call lib function to check for duplicate instances of this script

checkForDuplicateScript

sleep 10

exit 0

output from instance 1
=================
Count 1: test.sh
1

output from instane 2
================
Count 1: test.sh
2
Extra instance found of script: test.sh : Full ps output:
amon      4856  2.0  0.0  12316  1656 pts/0    S+   21:11   0:00 /bin/bash
./test.sh
amon      4870  2.0  0.0  12312  1660 pts/1    S+   21:11   0:00 /bin/bash
./test.sh
amon      4884  0.0  0.0   9140  1020 pts/1    S+   21:11   0:00 grep
test.sh
Instance Count: 3 : 4870 : test.sh


For some reason the count is being incremented yet ps lists the expected
output.
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.

The main question is Why when I assign it to a variable is INSTANCECOUNT being
incremented by one (giving 3 rather than 2)?

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...

Any thoughts from people on this list?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/kent/attachments/20111220/2519925c/attachment.htm>


More information about the Kent mailing list