简体   繁体   English

nodejs:获取守护程序的子进程并杀死它们

[英]nodejs: get child processes of a daemon and kill them

i want to build a node app which allows me to send kill -9 to all child processes of one daemon. 我想构建一个节点应用程序,使我可以将kill -9发送到一个守护程序的所有子进程。

To be clear. 要清楚。 We have one daemon on our server. 我们的服务器上有一个守护程序。 At its start it launches a process for the communication with our clients. 从一开始,它就启动了与客户沟通的过程。

When the client sends a new job to the server, a new child process is created by the daemon. 当客户端将新作业发送到服务器时,守护程序将创建一个新的子进程。

So now I want to get all child processes of the daemon, kill -9 them and then restart the daemon with systemctl restart mydaemon.service 因此,现在我想获取该守护程序的所有子进程,将它们kill -9 ,然后使用systemctl restart mydaemon.service启动该守护进程systemctl restart mydaemon.service

I searched google and did not find anything that fits my issue. 我搜索了google,但没有找到适合我问题的内容。

What I need to say is, i want to solve this without knowing the daemons process-id, of course only if possible. 我要说的是,我想在不知道守护进程process-id的情况下解决此问题,当然只有在可能的情况下。

Why do I need this 为什么我需要这个

Why I need to do this is because, the software the daemon belongs to, is buggy. 之所以需要执行此操作,是因为该守护程序所属的软件存在故障。 The communication process I mentioned above is failing and is simply gone. 我上面提到的通信过程失败了,只是消失了。 The seller says killing all processes is possible by just restarting the daemon, which of course is not. 卖方说,仅通过重新启动守护进程就可以杀死所有进程,而这当然不是。 So because the seller can't provide any solution for our problem were currently restarting the service the same way I want to automate it now. 因此,由于卖方无法为我们的问题提供任何解决方案,因此目前正在以我现在要使其自动化的方式重新启动该服务。 Killing all childs with SIGKILL and then restart the daemon. 使用SIGKILL杀死所有孩子,然后重新启动守护程序。

Thank you very much guys. 非常感谢你们。

You can find all the child processes (recursively) using a pstree utility. 您可以使用pstree实用程序(递归)找到所有子进程。 Mostly likely will need to install it. 通常很可能需要安装它。 On Mac, for example, you'd do: brew install pstree . 例如,在Mac上,您可以这样做: brew install pstree

Then you can run this snippet to find all the child processes and kill them: 然后,您可以运行此代码段以查找所有子进程并杀死它们:

 const child_process = require('child_process'); const { promisify } = require('util'); const execAsync = promisify(child_process.exec); (async () => { const pids = await execAsync( `pstree ${process.pid} | sed 's/[^0-9]*\\\\([0-9]*\\\\).*/\\\\1/' | grep -v "${process.pid}"` ); // Join the pids into one line separated by space const pidsString = pids.stdout.replace(/[\\r\\n]+/g, ' '); await execAsync(`kill -9 ${pidsString} || true`); })(); 

Please find the detailed explanation below: 请在下面找到详细说明:

  • pstree ${process.pid} - returns a tree of all the child processes. pstree ${process.pid} -返回所有子进程的树。 The output looks like this: 输出看起来像这样: 在此处输入图片说明

  • sed 's/[^0-9]*\\\\([0-9]*\\\\).*/\\\\1/' - keeps only the pids , removes the rest of the strings sed 's/[^0-9]*\\\\([0-9]*\\\\).*/\\\\1/' -仅保留pids ,删除其余字符串

  • grep -v "${process.pid}" - removes the current process from the list, we don't want to kill it grep -v "${process.pid}" -从列表中删除当前进程,我们不想杀死它

  • kill -9 ${pidsString} || true kill -9 ${pidsString} || true - kills the child processes with SIGKILL . kill -9 ${pidsString} || true -杀死与子进程SIGKILL

I had to do || true 我必须做|| true || true because pstree returns a full list of processes including itself (it also spawns ps internally). 因为pstree返回包含自身的pstree的完整列表(它也在内部生成ps ),所以为|| true Those processes are already ended at the time when we start killing so we need it to suppress the No such process errors. 这些进程在我们开始杀死程序时已经结束,因此我们需要它来消除“ No such process错误。

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

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