最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Child process not getting killed with Node.JS - Stack Overflow

programmeradmin21浏览0评论

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();
}

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();
}
Share Improve this question edited Jul 26, 2011 at 4:38 Ghostoy asked Jul 26, 2011 at 3:12 GhostoyGhostoy 2,7591 gold badge19 silver badges18 bronze badges 8
  • you can try to kill them manually (probably P2 ignores SIGHUP) - stackoverflow.com/questions/392022/… – Andrey Sidorov Commented Jul 26, 2011 at 3:30
  • @AndreySidorov How to kill manually w/o knowing its PID? – Ghostoy Commented Jul 26, 2011 at 4:38
  • you know it - ls.pid ( github.com/joyent/node/blob/master/lib/child_process.js#L250 ) – Andrey Sidorov Commented Jul 27, 2011 at 0:22
  • @AndreySidorov I mean PID of P2 since kill P1 does not make P2 exits. – Ghostoy Commented Jul 27, 2011 at 1:51
  • you can try to pass group id when spawn p2 and use it to kill – Andrey Sidorov Commented Jul 27, 2011 at 23:39
 |  Show 3 more comments

3 Answers 3

Reset to default 4

The safest way I've found to achieve this is creating a spawn process to kill the signal, using the child.pid as argument. Example with fork :

var process;
process = require('child_process');

var fork, child;
fork = process.fork;
child = fork('./index');

var spawn;
spawn = process.spawn;
spawn('kill', [child.pid]);
console.log('sending SIGTERM to child process (socket server)');

I typically use this in specs, mostly in beforeAll/afterAll blocks to start/kill servers (in the example './index.js').

In my case the problem was the shell being used for the process. By default it's /bin/sh, and when this was being used it wasn't handling the kill signals.

To fix this you can use /bin/bash instead:

exec('your command', {
    shell: '/bin/bash',
});

I think your P2 is neither a fork or a child of P1 but rather a parallel process spawned by it. In the case of Firefox ( at least in Linux ) the firefox app is launched by a wrapper shell script. If it's always going to be firefox, better run the binary straight from it's install folder

发布评论

评论列表(0)

  1. 暂无评论