How can I kill a whole process tree with Perl?

纵然是瞬间 提交于 2019-12-08 19:39:53

问题


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.

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?


回答1:


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. 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;



回答2:


CPAN has an answer. 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




回答3:


I adapted and hacked "rkill" to do the job, it was easy.

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

This is part of "pslist" package in e.g. ubuntu.



来源:https://stackoverflow.com/questions/305905/how-can-i-kill-a-whole-process-tree-with-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!