How to kill childprocess in nodejs?

node.jsShellChild Process

node.js Problem Overview


Created a childprocess using shelljs

!/usr/bin/env node

require('/usr/local/lib/node_modules/shelljs/global');
   fs = require("fs");  
   var child=exec("sudo mongod &",{async:true,silent:true});
      
   function on_exit(){
    	console.log('Process Exit');
    	child.kill("SIGINT");
    	process.exit(0)
    }
  
    process.on('SIGINT',on_exit);
    process.on('exit',on_exit);
    

Child process is still running .. after kill the parent process

node.js Solutions


Solution 1 - node.js

If you can use node's built in child_process.spawn, you're able to send a SIGINT signal to the child process:

var proc = require('child_process').spawn('mongod');
proc.kill('SIGINT');

An upside to this is that the main process should hang around until all of the child processes have terminated.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeepak PatilView Question on Stackoverflow
Solution 1 - node.jsMichael TangView Answer on Stackoverflow