如何使用Perl杀死整个进程树? [英] How can I kill a whole process tree with Perl?

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

问题描述

从Perl脚本中杀死进程及其所有子进程的最佳方法是什么?它至少应在Linux和Solaris下运行,并且不需要安装任何其他软件包.

What's the best way to kill a process and all its child processes from a Perl script? It should run at least under Linux and Solaris, and not require installation of any additional packages.

我的猜测是通过解析/proc中的文件或解析ps的输出(在Linux和Solaris之间都不是可移植的)来获得所有进程及其父级的列表.然后杀死树中的所有进程(这似乎很容易出现竞争状况).

My guess would be to get a list of all processes and their parents by parsing files in /proc or by parsing the output of ps (neither of which seems portable between Linux and Solaris); and then killing all processes in the tree (which seems prone to race conditions).

在这种情况下,我可以忍受比赛条件,但是如何便携地获得过程清单?

I could live with the race conditions in this particular case, but how do I portably get the process list?

推荐答案

如果您可以杀死进程组,则可以使用以下命令:

If you can live with killing a process group, you can use the following:

kill -$signum, $pgid;

其中,$signum是信号编号,而$pgid是进程组ID.但是,信号编号​​不是很容易携带,在这种情况下,您可以(在某些平台上;请阅读perlfunc进行解释)执行以下操作(例如,发送SIGTERM):

where $signum is the signal number, and $pgid is the process group ID. However, signal numbers aren't very portable, in which case you can (on some platforms; read perlfunc for explanation) do the following (to send SIGTERM, for example):

kill 'TERM', -$pgid;

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

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