Reputation: 3864
I've created a bash
script which counts launched instances of itself.
Here it is (in this example, I'm showing the instances rather than counting them with wc -l
) :
#!/bin/bash
nb=`ps -aux | grep count_itself.sh`
echo "$nb"
sleep 20
(Of course, my script is named count_itself.sh
)
Upon executing it, I expect it to return two lines, but it returns three :
root@myserver:/# ./count_itself.sh
root 16225 0.0 0.0 12400 1176 pts/0 S+ 11:46 0:00 /bin/bash ./count_itself.sh
root 16226 0.0 0.0 12408 564 pts/0 S+ 11:46 0:00 /bin/bash ./count_itself.sh
root 16228 0.0 0.0 11740 932 pts/0 S+ 11:46 0:00 grep count_itself.sh
Upon executing it with the &
flag (i.e. in background, and manually executing the ps -aux
bit, it returns two, which is what I want :
root@myserver:/# ./count_itself.sh &
[1] 16233
root@myserver:/# ps -aux | grep count_itself.sh
root 16233 0.0 0.0 12408 1380 pts/0 S 11:48 0:00 /bin/bash ./count_itself.sh
root 16240 0.0 0.0 11740 944 pts/0 S+ 11:48 0:00 grep --color=auto count_itself.sh
My question is : Why does the ps -aux
execution inside the script return one line more than expected ?
Or, in another words, why is the process with the id 16226
created in my first example ?
EDIT (as most people seem to misunderstand my question) :
I'm wondering why the bash
execution returns two instances of /bin/bash ./count_itself.sh
, not why it returns grep count_itself.sh
.
EDIT 2 :
And of course, I'm looking for a way to avoid this behaviour and have the script return /bin/bash ./count_itself.sh
only once.
Upvotes: 4
Views: 693
Reputation: 3864
Finally found a way, albeit an ugly one, partially inspired from the question @TomFenech linked in his answer:
#!/bin/bash
nb=$(ps f | grep '[c]ount_itself.sh' | grep -v ' \\_')
echo "$nb"
sleep 20
Execution :
root@myserver:/# ./count_itself.sh
17725 pts/1 S+ 0:00 \_ /bin/bash ./count_itself.sh
Execution with one already running in bg :
root@myserver:/# ./count_itself.sh &
[1] 17733
root@myserver:/# ./count_itself.sh
17733 pts/1 S 0:00 \_ /bin/bash ./count_itself.sh
17739 pts/1 S+ 0:00 \_ /bin/bash ./count_itself.sh
Explanation (from what I've understood) :
ps f
returns the tree of active processesgrep '[c]ount_itself.sh'
restricts the previous command to only showing instances of count_itself.sh
Returns
17808 pts/1 S+ 0:00 \_ /bin/bash ./count_itself.sh
17809 pts/1 S+ 0:00 \_ /bin/bash ./count_itself.sh
grep -v ' \\_'
excludes rows containing 4 spaces (equivalent of a tab) then \_
, which correspond to subprocessesUpvotes: 2
Reputation: 1021
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)
Upvotes: 2
Reputation: 32474
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/bash
a=1
b="$(a=2; echo abc)"
echo "a=$a"
echo "b=$b"
$ ./test.sh
a=1 # Note that the variable 'a' preserved its value
b=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.
Upvotes: 2
Reputation: 74605
This is a standard issue with grep
ing 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.
Upvotes: 5