使用节点 js 关闭.exe文件



我正在使用这个节点js代码来运行包含无限循环的App.exe文件。我传递输入并从应用程序.exe获取输出。

var bat = null;
app.post("/api/run", function(req, res) {
if(req.body.success) {
if(bat) bat.kill();
}
if(!bat) {
bat = spawn('cmd.exe', ['/c App.exe']);
bat.stderr.on('data', function (data) {
res.end(JSON.stringify({error: true, message: data.toString()}));
});
bat.on('exit', function (code) {
bat = null;
console.log('Child exited with code ' + code);
res.end(JSON.stringify({error: false, message: "Application Closed!"}));
});
}
bat.stdin.write(req.body.input+'n');
bat.stdout.once('data', function (data) {
console.log(data.toString());
res.end(JSON.stringify({error: false, message: data.toString()}));
});
});

问题是,当我杀死子进程子进程时成功,子进程被杀死,但App.exe继续运行。我有什么办法阻止应用程序.exe运行

与其生成生成App.execmd.exe,不如直接生成后者:

bat = spawn('App.exe');

为了杀死节点中生成的进程.js您需要使用SIGINT

bat.kill('SIGINT');

所有POSIX信号及其作用的列表可以在signal7中找到

最新更新