Preventing processes running concurrently - using ps
I have a BASH script that is activated by CRON. Inside this script there are other BASH scripts that will be running from within.
The scripts process files. The schedule is every half hour. If one script is already running, I cannot allow another one to run. This can happen if there are a lot of files to process at one time.
TO avoid this, I put something like this in each parent and child script:
#check if the same process is running
numpid=`/bin/ps -ef|grep convxml | grep -v grep|wc -l`
echo "convxml number of processes::"$numpid
if [ $numpid -gt 3 ] ; then
echo convxml already running..Terminating
exit 1
fi
Now, I am having a problem with this because $numpid is giving different values each time, when I thought it should just be giving one value. I have no idea why each time it is giving different values when each time there is no other process running.
This makes it difficult for me to check if there are any other processes running. When I dump `ps -ef` to a text file, within the script, I only see one occurence of 'convxml.sh' so why is it showing different values each time?
Any ideas?
convxml.sh number of processes:: 3
convxml.sh number of processes:: 2
convxml.sh number of processes:: 2
convxml.sh number of processes:: 2
convxml.sh number of processes:: 5
convxml.sh number of processes:: 2
convxml.sh number of processes:: 2
convxml.sh number of processes:: 5
convxml.sh number of processes:: 6
convxml.sh number of processes:: 2
|