I used child_process.exec
/ child_process.spawn
to fork a new process and then kill it using child.kill
/ process.kill
. It works fine with simple binary executables, such as cat
/ ls
, and the child process just get killed.
However, when get to the scripts (say P1) that forks another child process (say P2), only the script interpreter P1 get killed, not the child process P2.
QUESTION: is there any way that getting such child process P2 killed with Node.JS?
Code works fine with run_and_kill('ls -Al /usr/lib')
, but not OK with run_and_kill('firefox')
:
function run_and_kill(cmd) {
var exec = require('child_process').exec,
ls = exec(cmd);
console.log('Child process started: %d', ls.pid);
ls.on('exit', function(code, signal) {
console.log('exit with code %s and signal %s', code, signal);
});
ls.kill();
}