98

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

Deepak Patil
  • 3,419
  • 3
  • 25
  • 22
  • 9
    You're running `mongod &` which forks the process and sends it to background. sending **SIGINT** won't kill the actual `mongod` process. – fardjad Nov 25 '13 at 08: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 Nov 25 '13 at 08:16
  • 1
    removed '&' and it worked .... thanks http://stackoverflow.com/users/303270/fardjad – Deepak Patil Nov 25 '13 at 13:43
  • @fardjad, So how to deal with it? We all know what you said. It is written here, no secret https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal. But to deal with it? Do you know? – Green Jan 11 '18 at 07:04
  • @Green Maybe this helps: https://www.npmjs.com/package/ps-tree – fardjad Jan 12 '18 at 08:20

3 Answers3

101

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.

Michael Tang
  • 4,686
  • 5
  • 25
  • 24
  • 5
    isn't proc.kill() enough? or do we need proc.kill('SIGINT')? – Alexander Mills Oct 11 '16 at 04:44
  • i don't remember the specifics of `mongod`, but sending `proc.kill()` is (currently) equivalent to sending `proc.kill('SIGTERM')` (i'm assuming i believed `SIGINT` would shut down `mongod`?). i imagine that one would pick the correct signal for their particular application. – Michael Tang Oct 14 '16 at 05:46
  • 54
    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 May 18 '17 at 06: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.` https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal – Green Jan 11 '18 at 07: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 – Kirill Husiatyn Feb 26 '18 at 11:13
  • 1
    This does not kill the actual process on some cases if the option `shell` is `true` – AliFurkan Oct 30 '20 at 07:51
  • 5
    @fishbone, no it's not the same. Read it again. The answer suggests using `spawn` instead of `exec`. – Christian Fritz May 20 '21 at 21:52
  • @Green - Good point, but in the cases where it works, it is perfect. – Craig Hicks Feb 22 '22 at 20:27
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;
김동철
  • 1
  • 1
0

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.