简体   繁体   中英

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

PM2 process manager seems to have the key, because sending a SIGKILL to pm2 Daemon causes all child processes to exit (on Linux platform).

So do you know how PM2 achieve this ? 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 - Process - exit event

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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