I\'m writing a script (show_volume.sh) which may be called several times in short intervals. I need a way to determine whether there is more than just one running instance o
At the moment ps
runs a process grep show_volume.sh
is also running, so you always count the grep!
Simple solution, grep s[h]ow_volume.sh
. The grep (not shell) will collapse the [h]
to h
and search for what you want, but your grep won't match itself because it will have [] in the parameter.
pgrep
is also useful for this task and is smart enough to always exclude itself.
If you're running the script as different users then ps -a
will only show instances for the current user and only those with an attached terminal. Use ps -ax
or ps -e
.
pgrep -c
will show a count without having to use wc
.
The solution provided by ajreal:
ps -a | grep show_volume.sh | grep -v grep | wc -l
should work. If it does not, please provide output of
ps -a | grep show_volume.sh | grep -v grep
here
Try to exclude grep as well, as your grep itself also contains show_volume.sh
, an example
ps -a | grep show_volume.sh | grep -v grep | wc -l