简体   繁体   English

如何用Perl杀死整个进程树?

[英]How can I kill a whole process tree with Perl?

What's the best way to kill a process and all its child processes from a Perl script? 从Perl脚本中删除进程及其所有子进程的最佳方法是什么? It should run at least under Linux and Solaris, and not require installation of any additional packages. 它应该至少在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); 我的猜测是通过解析/ proc中的文件或解析ps的输出来获取所有进程及其父进程的列表(在Linux和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;

where $signum is the signal number, and $pgid is the process group ID. 其中$signum是信号编号, $pgid是进程组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): 但是,信号编号​​不是很便携,在这种情况下你可以(在某些平台上读取perlfunc进行解释)执行以下操作(例如,发送SIGTERM ):

kill 'TERM', -$pgid;

CPAN has an answer. CPAN有答案。 Yes, I know you did not want to install additional modules, but at least you can look at the implementation and see what they are doing... 是的,我知道你不想安装额外的模块,但至少你可以查看实现并看看他们在做什么......

https://metacpan.org/pod/Proc::ProcessTable https://metacpan.org/pod/Proc::ProcessTable

I adapted and hacked "rkill" to do the job, it was easy. 我改编并攻击了“rkill”来完成这项工作,很容易。

https://gitlab.com/pslist/pslist/blob/master/pslist https://gitlab.com/pslist/pslist/blob/master/pslist

This is part of "pslist" package in eg ubuntu. 这是ubuntu中“pslist”包的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM