使用node js关闭.exe文件
Close .exe File using node js
我正在将此节点 js 代码用于 运行 App.exe 包含无限循环的文件。我传递输入并从 App.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 保持 运行ning。我有什么办法可以阻止 App.exe 从 运行ning
为了终止在 node.js 中生成的进程,您需要使用 SIGINT
.
bat.kill('SIGINT');
可以在 signal7
中找到所有 POSIX 信号及其作用的列表
您可以直接生成后者,而不是生成 cmd.exe
生成 App.exe
:
bat = spawn('App.exe');
我正在将此节点 js 代码用于 运行 App.exe 包含无限循环的文件。我传递输入并从 App.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 保持 运行ning。我有什么办法可以阻止 App.exe 从 运行ning
为了终止在 node.js 中生成的进程,您需要使用 SIGINT
.
bat.kill('SIGINT');
可以在 signal7
中找到所有 POSIX 信号及其作用的列表您可以直接生成后者,而不是生成 cmd.exe
生成 App.exe
:
bat = spawn('App.exe');