Bash script counting instances of itself wrongly Bash script counting instances of itself wrongly bash bash

Bash script counting instances of itself wrongly


This is a standard issue with greping the output of ps.

One solution is to add some square brackets around a character

nb=$(ps -aux | grep '[c]ount_itself.sh')

This means that your grep instance doesn't match itself, because the name of the process with its arguments contains square brackets but the pattern that it matches doesn't.

As mentioned in the comments, you should use double quotes around your variables in order to preserve whitespace.

The reason why you have appear to have two instances of the same shell in your results is that the command substitution is executed within a subshell. For details on only showing the parent process, see this question.


Process substitution requires the parent shell to start a sub-shell, i.e. to fork and execute the specified commands in a child shell. This is necessary so that the parent shell is unaffected by any changes to the environment (variables, current working directory, traps), that the script enclosed in $(...) makes.

Example:

$ cat test.sh #!/bin/basha=1b="$(a=2; echo abc)"echo "a=$a"echo "b=$b"$ ./test.sh a=1           # Note that the variable 'a' preserved its valueb=abc

It is as a result of forking that you are seeing an extra instance of your script.

I don't think that it is possible to reliably eliminate those unwanted processes from your output, since, in principle, a script may legitimately start another instance of itself (which will run as a subprocess), and you cannot distinguish between those two cases.

One hacky solution is to have the script create at a designated location (e.g. in /tmp/your_script_name) a PID file upon invocation and remove it upon termination.


I suggest next way:

Exclude all process that parent are myself:

 ps --pid $$ -N -a | grep count_itself.sh

This means show all commands that parent are not myself (so this exclude your grep process and your fork process to execute counter sentence)