102

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

5
  • 9
    You're running mongod & which forks the process and sends it to background. sending SIGINT won't kill the actual mongod process.
    – fardjad
    Commented Nov 25, 2013 at 8:11
  • 2
    One more thing, instead of executing sudo mongod, exec mongod and run your script with sudo. You can drop privileges after executing mongod (with process.setuid() and process.setgid()) if you want.
    – fardjad
    Commented Nov 25, 2013 at 8:16
  • 1
    removed '&' and it worked .... thanks stackoverflow.com/users/303270/fardjad Commented Nov 25, 2013 at 13:43
  • @fardjad, So how to deal with it? We all know what you said. It is written here, no secret nodejs.org/api/…. But to deal with it? Do you know?
    – Green
    Commented Jan 11, 2018 at 7:04
  • @Green Maybe this helps: npmjs.com/package/ps-tree
    – fardjad
    Commented Jan 12, 2018 at 8:20

3 Answers 3

102

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.

8
  • 6
    isn't proc.kill() enough? or do we need proc.kill('SIGINT')? Commented Oct 11, 2016 at 4:44
  • 57
    How does this answer the question? This answer suggests exactly the same what the author of the question already does to kill the process. Nevertheless it gets that many upvotes?
    – fishbone
    Commented May 18, 2017 at 6:22
  • 6
    WRONG! You can't terminate a service you have started in a child process. You can kill child process but cannot terminate a service. That's the problem ` the signal delivered to the child process may not actually terminate the process.` nodejs.org/api/…
    – Green
    Commented Jan 11, 2018 at 7:10
  • 4
    Thank you, this saved my time! Also, I had a problem that my forked process spawned another processes (with cluster) and .kill('SIGINT') did the trick and close every child and subchild processes correctly for me in my electron-nodejs application Commented Feb 26, 2018 at 11:13
  • 5
    @fishbone, no it's not the same. Read it again. The answer suggests using spawn instead of exec. Commented May 20, 2021 at 21:52
3

The ONLY thing that has worked for me to kill an .exec() child process is to use the npm library terminate. Here's how:

const terminate = require('terminate')
terminate(myProcess.pid, err => console.log(err))

Where myProcess is simply this: const myProcess = childProcess.exec(...).

You can install terminate with: yarn add terminate

This worked perfectly.

0
// only windows

const module_child_process = require('child_process');
let proc = module_child_process.spawn("cmd"); 
let pid = proc.pid;
let cmd = `wmic process where parentprocessid=${pid} get ProcessId`;
let out = module_child_process.execSync(cmd);
let stdout = out.toString();
let pids=[];
let lines = stdout.split("\n");
for (let i = 0; i < lines.length; i++) 
{
    try 
    {
        let data = lines[i].toString();
        let val = parseInt(data.trim());
        if (!isNaN(val))
        {
            //console.log(val.toString());
            pids.push(val);
        }
    }
    catch (ex)
    {
    }
}

for (let i = 0; i < pids.length; i++) 
{
    module_process.kill(pids[i]);
    /* or    
    let cmd = `taskkill /pid ${pids[i].toString()} /T /F`;
    console.log(cmd);
    module_child_process.execSync(cmd);
    */
}

proc.kill();
proc = null;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.