Bash:在执行bash之前等待子进程生成

时间:2018-08-16 13:31:58

标签: bash shell

Bash可以使用wait等待直接启动的进程。但是,如果进程派生了一个子进程,然后执行了bash(即,父进程变成了bash),则新执行的Bash进程将无法等待“继承的”子进程。这是最小的复制品:

#/bin/bash
sleep inf &
pid=$!
exec bash -c "wait $pid;"'echo This shell is $$; sleep inf'

给出以下输出:

$ bash test.sh
bash: wait: pid 53984 is not a child of this shell
This shell is 53983

但是,pstree显示子pid确实是shell的子:

$ pstree -p 53983
bash(53983)─┬─sleep(53984)
            └─sleep(53985)

似乎Bash在内部跟踪生成的进程,并查询该列表,而不是直接调用waitpid(2)zsh存在相同的问题,但是ksh可以正常工作)。 是否有任何方法可以解决此问题,并使Bash将“继承的”子代添加到其内部结构中?

0 个答案:

没有答案
相关问题