7

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.

3 Answers 3

9

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.

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:

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
2

You could try running ps u instead. From the man pages, the u option corresponds to display user-oriented format which will display both the shell used to run the script and the name of the script itself.

1

Apparently, it helps if I add !#/bin/bash at the beginning of the file... Then the name of the script appears in the ps report.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .