Search code examples
javascriptnode.jschild-processshutdownspawn

Does the child process in Node JS need to be killed manually?


I have a child process.

The Questions is:

  • Does the child process need to be killed manually?
  • Will the child process continue to run even if my application is shut down?
  • Will the child process automatically killed if the main application dies?

Example

import { spawn } from 'child_process'

const backTaskProcess = spawn(process.execPath, ['back-task.js'], {
  cwd: process.cwd(),
})

// Do i need to disabled it manually?
process.on('SIGQUIT', () => {
  backTaskProcess.kill('SIGQUIT')
})

Solution

  • No, no, and yes. You don't need to kill it manually, unless you use detached: true in the options. There are exceptions when using fork(), but that doesn't apply to your code.