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

Killing a NodeJS Child Process with child.kill('SIGHUP')

这是我的代码:

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 中的说明编写了这段代码。但是没用。

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

我使用 tree-kill 模块终止了 Windows 上的子进程。

这样使用:

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