简体   繁体   English

Nodejs子进程和SIGKILL

[英]Nodejs child process and SIGKILL

I am looking for a way to kill all child processes spawned when my parent process die (including by receiving SIGKILL). 我正在寻找一种方法来杀死我父进程死亡时产生的所有子进程(包括接收SIGKILL)。

PM2 process manager seems to have the key, because sending a SIGKILL to pm2 Daemon causes all child processes to exit (on Linux platform). PM2进程管理器似乎有密钥,因为向pm2守护进程发送SIGKILL会导致所有子进程退出(在Linux平台上)。

So do you know how PM2 achieve this ? 所以你知道PM2是如何实现这一目标的吗? how can I reproduce this behavior ? 我该如何重现这种行为?

Thanks ! 谢谢 !

'use strict'

const { fork } = require('child_process')

// fork a child process
const child = fork('new_process.js')

// when the main process exits, send the kill signal to the child process
process.on('exit', (code) => {
  console.log(`About to exit with code: ${code}`)
  child.kill(9)
})

Usefull docs 有用的文档

NodeJS - Child Process - kill() NodeJS - 子进程 - kill()

NodeJS - Process - exit event NodeJS - 进程 - 退出事件

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

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