Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill childprocess in nodejs?

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

like image 500
Deepak Patil Avatar asked Nov 25 '13 07:11

Deepak Patil


People also ask

How do I kill a NodeJS process?

To kill the main Node process, we just pass the pid of the main process. To see this in operation, replace the setTimeout function in our previous code example with this version that uses process. kill . This method also works in the REPL as well as in Node.

How do you kill a child's process?

For killing a child process after a given timeout, we can use the timeout command. It runs the command passed to it and kills it with the SIGTERM signal after the given timeout. In case we want to send a different signal like SIGINT to the process, we can use the –signal flag.


1 Answers

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.

like image 134
Michael Tang Avatar answered Sep 20 '22 02:09

Michael Tang