使用child.kill杀死NodeJS子进程(' SIGHUP')

时间:2018-01-02 14:01:00

标签: javascript node.js child-process

这是我的代码:

test.js

const {exec} = require("child_process")

var c = exec("php artisan serve", {
        cwd: "C:/Users/DELL/Laravel Projects/lktest3"
    }, (error, stdout, stderr) => {
        if (error) {
            console.error(`exec error: ${error}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
    console.log(`stderr: ${stderr}`);
})
setTimeout(() => {
    c.kill('SIGHUP')
}, 10000);

当我运行node test.js时,我收到此错误:

$ node test.js
internal/child_process.js:397
      throw errnoException(err, 'kill');
      ^

Error: kill ENOSYS
    at exports._errnoException (util.js:1018:11)
    at ChildProcess.kill (internal/child_process.js:397:13)
    at Timeout.setTimeout (C:\Users\DELL\Documents\laravel-kit\test.js:14:7)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)

我在NodeJS Child Process API中说过这段代码。但它没有用。

2 个答案:

答案 0 :(得分:0)

使用childProcess.spawn(command)docs)而不是childProcess.exec,因为“exec”会创建一个新的shell并在该shell中运行该命令。

答案 1 :(得分:0)

我使用tree-kill模块来杀死Windows上的子进程。

像这样使用:

var kill = require('tree-kill');
kill(your_child_process.pid, 'SIGKILL', function(err) {
    // Do things
});
相关问题