You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用child.kill('SIGHUP')终止Node.js子进程报错求助

解决Node.js Child Process在Windows上kill报错ENOSYS的问题

这个问题的核心原因很明确:Windows系统不支持Unix/Linux下的SIGHUP信号,当你调用c.kill('SIGHUP')时,Node.js在Windows平台无法识别这个信号,就会抛出ENOSYS错误(表示系统不支持该操作)。

下面给你两种可行的解决方案:

方案1:使用默认终止信号

直接调用不带参数的kill(),Node.js会自动根据当前系统选择合适的终止信号(Windows上会触发等效的进程终止逻辑):

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(() => { 
  // 不指定信号,使用默认的SIGTERM
  c.kill(); 
}, 10000);

方案2:使用Windows支持的信号

Windows原生支持SIGINT信号(效果等同于手动按下Ctrl+C),这个信号可以正常终止php artisan serve这类前台运行的进程:

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(() => { 
  // 使用Windows支持的SIGINT信号
  c.kill('SIGINT'); 
}, 10000);

额外小提示:为了避免进程已经提前退出后调用kill引发额外错误,你可以加个简单的状态判断:

setTimeout(() => { 
  if (!c.killed) {
    c.kill('SIGINT');
  }
}, 10000);

内容的提问来源于stack exchange,提问作者Tareque Md Hanif

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:49:12