ps
returns 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.
ps
returns 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.
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 isbash
. If you run it asTestEric.sh
(possibly including its path), the base command will be that and not bash.You can easily use
pgrep
instead ofps
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.The
-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.Try this:
You can later count the processes using
wc
by counting thepgrep
output lines:You could try running
ps u
instead. From the man pages, the u option corresponds todisplay user-oriented format
which will display both the shell used to run the script and the name of the script itself.Apparently, it helps if I add
!#/bin/bash
at the beginning of the file... Then the name of the script appears in theps
report.