Bash script counting instances of itself wrongly

荒凉一梦 提交于 2019-12-05 17:49:15
Tom Fenech

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/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.

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)

roberto06

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 processes
  • grep '[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 subprocesses
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!