如何杀死shell的所有子进程? [英] How to kill all subprocesses of shell?

查看:53
本文介绍了如何杀死shell的所有子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 bash 脚本,它可以做几件事.

I'm writing a bash script, which does several things.

一开始它会启动几个监视器脚本,每个脚本都运行一些其他工具.

In the beginning it starts several monitor scripts, each of them runs some other tools.

在我的主脚本结束时,我想杀死所有从我的 shell 中产生的东西.

At the end of my main script, I would like to kill all things that were spawned from my shell.

所以,它可能看起来像这样:

So, it might looks like this:

#!/bin/bash

some_monitor1.sh &
some_monitor2.sh &
some_monitor3.sh &

do_some_work
...

kill_subprocesses

问题是这些监视器中的大多数会产生自己的子进程,因此(例如):killall some_monitor1.sh 并不总是有帮助.

The thing is that most of these monitors spawn their own subprocesses, so doing (for example): killall some_monitor1.sh will not always help.

还有其他方法可以处理这种情况吗?

Any other way to handle this situation?

推荐答案

启动每个子进程后,可以用

After starting each child process, you can get its id with

ID=$!

然后您可以使用存储的 PID 来查找和终止所有孙进程等进程,如here这里.

Then you can use the stored PIDs to find and kill all grandchild etc. processes as described here or here.

这篇关于如何杀死shell的所有子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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