簡體   English   中英

使用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
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM