Possible Duplicate:
Quick-and-dirty way to ensure only one instance of a shell script is running at a time
I am new to shell script.
what I wanna do is to avoid running multiple instances of a script.
I have this shell script cntps.sh
#!/bin/bash
cnt=`ps -e|grep "cntps"|grep -v "grep"`
echo $cnt >> ~/cntps.log
if [ $cnt < 1 ];
then
#do something.
else
exit 0
fi
if I run it this way $./cntps.sh
, it echoes 2
if I run it this way $. ./cntps.sh
, it echoes 0
if I run it with crontab
, it echoes 3
Could somebody explain to me why is this happening? And what is the proper way to avoid running multiple instances of a script?