Hi,
I have a script which I run through cron every xx minutes. In the script I check if it is already running, if it is already running I need to exit the script.
I have used the below logic to get the number of instances. However occasionaly it is giving wrong values. Why does it happen and how do I fix it.
Below I have pasted the script and the output. The output of just the grep command is also pasted at the end. Any help is greatly appreciated.
SCRIPT:
Code:
#!/bin/sh
instance=`ps -ef|grep "/bin/sh"|grep "testSCRIPT.sh"|egrep -v '\-c|grep'|wc -l`
echo $instance
echo `date`
echo "######################"
OUTPUT:
2
Fri Feb 18 11:34:05 IST 2011
######################
2
Fri Feb 18 11:35:06 IST 2011
######################
1
Fri Feb 18 11:36:06 IST 2011
######################
3
Fri Feb 18 11:37:05 IST 2011
######################
1
Fri Feb 18 11:38:05 IST 2011
######################
OUTPUT OF GREP
root 24951 15758 0 13:05:50 pts/6 0:00 /bin/sh ./testSCRIPT.sh
root 24953 24951 0 13:05:50 pts/6 0:00 /bin/sh ./testSCRIPT.sh