bash 查找子进程数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11542243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:48:15  来源:igfitidea点击:

Finding number of child processes

bashprocesspid

提问by Adam Matan

How do find the number of child processes of a bash script, from within the script itself?

如何从脚本本身中找到 bash 脚本的子进程数?

采纳答案by betabandido

To obtain the PID of the bash script you can use variable $$.

要获取 bash 脚本的 PID,您可以使用变量$$.

Then, to obtain its children, you can run:

然后,要获得它的孩子,你可以运行:

bash_pid=$$
children=`ps -eo ppid | grep -w $bash_pid`

pswill return the list of parent PIDs. Then grepfilters all the processes not related to the bash script's children. In order to get the number of children you can do:

ps将返回父 PID 列表。然后grep过滤所有与 bash 脚本的子进程无关的进程。为了获得孩子的数量,您可以执行以下操作:

num_children=`echo $children | wc -w`

Actually the number you will get will be off by 1, since pswill be a child of the bash script too. If you do not want to count the execution of psas a child, then you can just fix that with:

实际上,您将获得的数字将减少 1,因为ps它也是 bash 脚本的子项。如果您不想将执行计算ps为孩子,那么您可以使用以下方法解决:

let num_children=num_children-1

UPDATE:In order to avoid calling grep, the following syntax might be used (if supported by the installed version of ps):

更新:为了避免调用grep,可能会使用以下语法(如果已安装的 版本支持ps):

num_children=`ps --no-headers -o pid --ppid=$$ | wc -w`

回答by nrc

I prefer:

我更喜欢:

num_children=$(pgrep -c -P$$)

num_children=$(pgrep -c -P$$)

It spawns just one process, you do not have to count words or adjust the numbers of PIDs by the programs in the pipe.

它只生成一个进程,您不必通过管道中的程序计算字数或调整 PID 的数量。

Example:

例子:

~ $ echo $(pgrep -c -P$$)
0
~ $ sleep 20 &
[1] 26114
~ $ echo $(pgrep -c -P$$)
1

回答by pretorh

You can also use pgrep:

您还可以使用 pgrep:

child_count=$(($(pgrep --parent $$ | wc -l) - 1))

child_count=$(($(pgrep --parent $$ | wc -l) - 1))

Use pgrep --parent $$to get a list of the children of the bash process.
Then use wc -lon the output to get the number of lines: $(pgrep --parent $$ | wc -l)Then subtract 1 (wc -lreports 1 even when pgrep --parent $$is empty)

使用pgrep --parent $$得到bash进程的孩子的名单。
然后wc -l在输出上使用以获取行数:$(pgrep --parent $$ | wc -l)然后减去 1(wc -l即使pgrep --parent $$为空也报告 1 )

回答by chepner

Use pswith the --ppidoption to select children of the current bash process.

ps--ppid选项一起使用以选择当前 bash 进程的子进程。

bash_pid=$$
child_count=$(ps -o pid= --ppid $bash_id | wc -l)
let child_count-=1    # If you don't want to count the subshell that computed the answer

(Note: this requires the Linux version of psfor --ppid. I don't know if there is an equivalent for BSD psor not.)

(注意:这需要 Linux 版本的psfor --ppid。我不知道 BSD 是否有等效的版本ps。)

回答by Jonny

If job count (instead of pid count) is enough I just came with a bash-only version:

如果作业计数(而不是 pid 计数)就足够了,我只提供了 bash-only 版本:

job_list=($(jobs -p))
job_count=${#job_list[@]}

回答by pdug

You may evaluate the shell builtin command jobs like:

您可以评估 shell 内置命令作业,例如:

counter = `jobs | wc -l`