如何等待所有子(和孙子等)由脚本生成的进程 [英] How to wait on all child (and grandchild etc) process spawned by a script

查看:117
本文介绍了如何等待所有子(和孙子等)由脚本生成的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:

用户提供我自己的自定义脚本来运行。这些脚本可以是任何形式的像脚本启动多个GUI程序,后端服务。我有过脚本编写方式无法控制。这些脚本可以会阻止类型,即执行等待,直到所有的子进程(即顺序运行的程序)的退出

Users provide me their custom scripts to run. These scripts can be of any sort like scripts to start multiple GUI programs, backend services. I have no control over how the scripts are written. These scripts can be of blocking type i.e. execution waits till all the child processes (programs that are run sequentially) exit

#exaple of blocking script
echo "START"
first_program 
second_program 
echo "DONE"

或非阻塞型,即那些在后台和退出像

or non blocking type i.e. ones that fork child process in the background and exit something like

#example of non-blocking script
echo "START"
first_program &
second_program &
echo "DONE"

什么是我想要达到什么目的?

用户提供的脚本可以是任何上述两种或两种的混合。我的工作是运行脚本,等到开始通过其退出,然后关闭该节点的所有进程。如果阻塞型的,情况朴素简单,即让脚本执行进程的PID,等到PS -ef | grep的-ef PID没有更多的条目。非阻塞的脚本是那些给我找麻烦

User provided scripts can be of any of the above two types or mix of both. My job is to run the script and wait till all the processes started by it exit and then shutdown the node. If its of blocking type, case is plain simple i.e. get the PID of script execution process and wait till ps -ef|grep -ef PID has no more entries. Non-blocking scripts are the ones giving me trouble

有没有一种方法我可以通过脚本的执行产生的所有子进程的PID的名单?任何指针或提示将是非常美联社preciated

Is there a way I can get list of PIDs of all the child process spawned by execution of a script? Any pointers or hints will be highly appreciated

推荐答案

您可以使用等待通过 userscript启动的所有后台进程来完成。由于仅适用于当前shell的孩子,你需要,而不是源运行它作为一个独立的过程中他们的脚本。

You can use wait to wait for all the background processes started by userscript to complete. Since wait only works on children of the current shell, you'll need to source their script instead of running it as a separate process.

( source userscript; wait )

在一个明确的子shell采购脚本应该模仿开始一个新的进程不够紧密。如果没有,你还可以后台子shell,这迫使一个新的进程来启动,那么等待的的完成。

Sourcing the script in an explicit subshell should simulate starting a new process closely enough. If not, you can also background the subshell, which forces a new process to be started, then wait for it to complete.

( source userscript; wait ) & wait

这篇关于如何等待所有子(和孙子等)由脚本生成的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆