Bash:当父脚本退出时,生成的子进程退出 [英] Bash: spawn child processes that quit when parent script quits

查看:114
本文介绍了Bash:当父脚本退出时,生成的子进程退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Bash中生成几个子进程,但是我希望父脚本保持运行状态,这样发送到父脚本的信号也会影响所生成的子进程.

I'd like to spawn several child processes in Bash, but I'd like the parent script to remain running, such that signals send to the parent script also affect the spawned children processes.

这不能做到这一点:

parent.bash :

#!/usr/bin/bash

spawnedChildProcess1 &
spawnedChildProcess2 &
spawnedChildProcess3 &

parent.bash 立即结束,并且所产生的进程继续独立于其运行.

parent.bash ends immediately, and the spawned processes continue running independently of it.

推荐答案

使用 wait 使父进程等待所有子进程退出.

Use wait to have the parent process wait for all the children to exit.

#!/usr/bin/bash

spawnedChildProcess1 &
spawnedChildProcess2 &
spawnedChildProcess3 &

wait

键盘信号被发送到整个进程组,因此键入 Ctl-c 将杀死子级和父级.

Keyboard signals are sent to the entire process group, so typing Ctl-c will kill the children and the parent.

这篇关于Bash:当父脚本退出时,生成的子进程退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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