使用节点js

时间:2017-08-26 07:09:29

标签: javascript node.js exe spawn

我正在使用此节点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仍在运行。我有办法阻止App.exe运行

2 个答案:

答案 0 :(得分:2)

而不是产生cmd.exe产生App.exe,而不是直接产生后者:

bat = spawn('App.exe');

答案 1 :(得分:0)

要在node.js中终止衍生进程,您需要使用SIGINT

bat.kill('SIGINT');

可以在signal7

找到所有POSIX信号及其功能的列表
相关问题