微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Bash:杀死subprocess中的所有进程

在bash中,我可以通过$!获得最后一个subprocess的进程ID( pid ) variables。 我可以在完成之前杀死这个subprocess:

(sleep 5) & pid=$! kill -9 $pid

这工作如广告。 如果现在在sleep之后用更多的命令扩展subprocess,则在subprocess被终止之后,即使其他命令从不被执行, sleep命令也会继续。

作为一个例子,考虑下面这个,它使用ps加速一个subprocess并监视它的暗杀行为:

# Start subprocess and get its pid (sleep 5; echo done) & pid=$! # grep for subprocess echo "grep before kill:" ps aux | grep "$pid|sleep 5" # Kill the subprocess echo echo "Killing process $pid" kill -9 $pid # grep for subprocess echo echo "grep after kill:" ps aux | grep "$pid|sleep 5" # Wait for sleep to finish sleep 6 # grep for subprocess echo echo "grep after sleep is finished:" ps aux | grep "$pid|sleep 5"

如果我把它保存到一个名为filename并运行它,我得到这个打印输出

parsingNginx访问日志并提取IP,检测每个parsing的IP的地理位置

Python subprocess.call函数不redirect输出

rsync排除`/ index.PHP`的模式,而不是`/ dir / subdir / index.PHP`

login后运行bash脚本

bash:如何拦截命令行,根据内容做各种动作?

grep before kill: username 7464 <...> bash filename username 7466 <...> sleep 5 username 7467 <...> grep 7464|sleep 5 Killing process 7464 grep after kill: username 7466 <...> sleep 5 username 7469 <...> grep 7464|sleep 5 grep after sleep is finished: username 7472 <...> grep 7464|sleep 5

ps命令中不重要的信息被replace为<...> 。 它看起来像kill已经杀死了filename的整体bash执行,而保持sleep运行。

我怎样才能正确地杀死整个subprocess?

awk列出一个特定的组

Linux:如何杀死睡眠

bash脚本从shell运行,而不是从cron作业运行

从Bash中另一个更大的文本文件中find文本文件的行的最快方法

最大数量的Bash参数!=最大数量cp参数?

您可以在子shell中设置一个陷阱,以便在退出之前终止任何活动的作业:

(trap 'kill $(jobs -p)' EXIT; sleep 5; echo done ) & pid=$!

你可以看看似乎满足你的要求的rkill :

http://www.unix.com/man-page/debian/1/rkill/

rkill [-SIG] pid / name …

当作为rkill被调用时,该实用程序不显示有关进程的信息,而是将它们全部发送给一个信号。 如果未在命令行中指定,则发送终止(SIGTERM)信号。

我不知道到底是为什么那个睡眠过程变成孤儿,反而是杀了你可以用-P标志来使用pkill 也 杀死所有的孩子

pkill -TERM -P $pid

编辑:这意味着,为了杀死一个进程,所有它的孩子,你应该使用

CPIDS=`pgrep -P $pid` # gets pids of child processes kill -9 $pid for cpid in $CPIDS ; do kill -9 $cpid ; done

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐