LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Preventing processes running concurrently - using ps (https://www.linuxquestions.org/questions/linux-general-1/preventing-processes-running-concurrently-using-ps-539176/)

TomCruise2002 03-20-2007 08:33 PM

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

MensaWater 03-21-2007 01:10 PM

Your shell script is convxml.sh. If it contains a command with a name like "convxml" or "convxml.pl" then it would count those because you're just grepping for "convxml" and not "convxml.sh".

It is fairly common for users to name their scripts after the binary they're wrapping it around so I wouldn't be surprised to find you were calling something called convxml (with or without a suffix) within the script itself.


All times are GMT -5. The time now is 08:03 PM.