bash as the name of the process... I would like the name of the shell script instead.
For instance, if my script is
TestEric.sh, I would like to list the number of instances of that script currently running.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
The problem here is that
ps by default only shows the base command in its output, not the whole command line including arguments.
If you launch your script with
bash TestEric.sh, the base command is
bash. If you run it as
TestEric.sh (possibly including its path), the base command will be that and not bash.
You can easily use
pgrep instead of
ps to list processes filtered by name. It has a
-f flag that makes it match the given pattern against the whole command line instead of just the base command.
-a flag makes it display that whole command line in its output for debugging purposes instead of just the PID. Once you verified it works, you can/should remove it again in your script.
pgrep -f -a TestEric.sh
You can later count the processes using
wc by counting the
pgrep output lines:
pgrep -f TestEric.sh | wc -l