Node.Js Child Processes Being Killed When Parent Dies

Node.JS child processes being killed when parent dies

you need to set the detached option

If the detached option is set, the child process will be made the
leader of a new process group. This makes it possible for the child to
continue running after the parent exits.

var child = spawn('prg', [], {
detached: true,
stdio: [ 'ignore', out, err ]
});

parent process kills child process, even though detached is set to true

Aha! Of course. Those stupid Node docs!

This works.

        const n = cp.spawn('node', ['lib/transpile/watch-transpile.js'], {
detached: true,
stdio: ['ignore', 'ignore', 'ignore']
});

You explicitly ignore each stdio stream, not just using 'ignore' once; the docs don't mention this directly, but it makes sense given that the stdio property is an array.

See this issue on Github: https://github.com/nodejs/node/issues/7269#issuecomment-225698625

kill all child_process when node process is killed

You need to listen for the process exit event and kill the child processes then. This should work for you:

var args = "ffmpeg -i in.avi out.avi"
var a = child_process.exec(args , function(err, stdout,stderr){});

var b = child_process.exec(args , function(err, stdout,stderr){});

process.on('exit', function () {
a.kill();
b.kill();
});

Node: child_process.exec() process continuing after parent process dies

Different operating systems handle child processes differently. I usually add in handler like this:

['SIGINT', 'SIGHUP', 'SIGTERM'].forEach(function(signal) {
process.addListener(signal, gracefulShutdown);
});

gracefulShutdown should do things like close sockets and quit processes (process.stop())

OH... And I just reread your question. ctrl-z pauses a process, it doesn't kill it. If you use fg or bg, it will bring the process back into the foreground/background. To quick the REPL, use ctrl-c twice.



Related Topics



Leave a reply



Submit