bash 使linux“等待”命令等待所有子进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2290156/
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
Making linux "Wait" command wait for ALL child processes
提问by Dan
Wait is not waiting for all child processes to stop. This is my script:
等待不是等待所有子进程停止。这是我的脚本:
#!/bin/bash
titlename=`echo "$@"|sed 's/\..\{3\}$//'`
screen -X title "$titlename"
/usr/lib/process.bash -verbose $@
wait
bash -c "mail.bash $@"
screen -X title "$titlename.Done"
I don't have access to /usr/lib/process.bash, but it is a script that changes frequently, so I would like to reference it... but in that script:
我无权访问/usr/lib/process.bash,但它是一个经常更改的脚本,所以我想引用它......但在该脚本中:
#!/bin/ksh
#lots of random stuff
/usr/lib/runall $path $auto $params > /dev/null 2>&1&
My problem is that runallcreates a log file... and mail.bashis suppose to mail me that log file, but the wait isn't waiting for runallto finish, it seems to only be waiting for process.bashto finish. Is there anyway, without access to process.bash, or trying to keep my own up to date version of process.bash, to make the wait properly wait for runallto finish? (The log file overwrites the previous run, so I can't just check for the presence of the log file, since there is always one there)
我的问题是runall创建了一个日志文件......并且mail.bash假设将那个日志文件邮寄给我,但是等待不是等待runall完成,它似乎只是在等待process.bash完成。无论如何,是否无法访问process.bash,或者尝试保持我自己的process.bash 的最新版本,以使等待正确等待runall完成?(日志文件覆盖了之前的运行,所以我不能只检查日志文件的存在,因为那里总是有一个)
Thanks, Dan
谢谢,丹
回答by ephemient
(
. /usr/lib/process.bash -verbose $@
wait
)
Instead of letting the OS start process.bash, this creates a subshell, runs all the commands in process.bashas if they were entered into our shell script, and waits within that subshell.
这不是让操作系统启动process.bash,而是创建一个子外壳,运行所有命令,process.bash就像它们被输入到我们的外壳脚本中一样,并在该子外壳中等待。
There are some caveats to this, but it should work if you're not doing anything unusual.
对此有一些警告,但如果您没有做任何不寻常的事情,它应该可以工作。
回答by Ignacio Vazquez-Abrams
waitonly waits for direct children; if any children spawn their own children, it won't wait for them.
wait只等待直系子女;如果任何孩子产生自己的孩子,它不会等待他们。
回答by John Kugelman
The main problem is that because process.bashhas exited the runallprocess will be orphaned and owned by init(PID 1). If you look at the process list runallwon't have any visible connection to your process any more since the intermediate process.bashscript exited. There's no way to use ps --ppidor anything similar to search for this "grandchild" process once it's orphaned.
主要问题是,因为process.bash已经退出,runall进程将被孤立并归init(PID 1)所有。如果您查看进程列表,runall则自中间process.bash脚本退出后,您的进程将不再有任何可见的连接。ps --ppid一旦它成为孤儿,就无法使用或类似的方法来搜索这个“孙子”进程。
You can waiton a specific PID. Do you know the PID of the runallprocess? If there's only one such process you could try this, which will wait for all running runalls:
你可以wait对一个特定的PID。你知道runall进程的PID吗?如果只有一个这样的进程,你可以试试这个,它会等待所有正在运行的runalls:
wait `pidof runall`
回答by Debugger
You could recuperate the PID of the process for whom you want to wait
您可以恢复要等待的进程的 PID
And then pass this PID as an argument to the command Wait
然后将此PID作为参数传递给命令Wait

