Karl Cunningham wrote:
>
> I'm writing a shell script that will have problems if another instance of
> itself is running, and I'd like to be able to trap that the user has
> started multiple instances. I've tried
>
> cnt=`ps ax | grep -c xyz`
>
> where xyz is the name of the script. It returns 3 when there is only one
> instance running. I think I understand why it's 3 (one for the script
> itself, one for it running ps and one for it running grep?). Will this
> always work? Is there a better way?
This works from command line...
# if [ `ps -efw | grep -v grep | grep rerf |wc -l` -eq 0 ] ; then
echo 'yes' ; else echo 'no' ; fi
In a shell script, make it more readable...
if [ `ps -efw | grep -v grep | grep rerf |wc -l` -eq 0 ]
then
echo 'yes'
# more statements here, etc.
else
echo 'no'
fi
-----
Also, analias command that I use *often* is
# alias psg
alias psg='ps -efw | grep -v grep | grep'
This avoids seeing the 'grep' that is forked off by your request.
Thanks...Dan