Saad Mehmood
Saad Mehmood

Reputation: 731

Close .exe File using node js

I am using this node js code to run App.exe file that contains an infinite loop. I pass input and get output from 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()}));
    });
});

Problem is that on success when I kill child process child process gets killed but App.exe keeps running. I there any way to stop App.exe from running

Upvotes: 2

Views: 1990

Answers (2)

Alexandru Olaru
Alexandru Olaru

Reputation: 7092

In order to kill a spawned process in node.js you need to use the SIGINT.

bat.kill('SIGINT');

A list of all the POSIX signals and what they do can be found at signal7

Upvotes: 0

robertklep
robertklep

Reputation: 203231

Instead of spawning cmd.exe which spawns App.exe, you can spawn the latter directly:

bat = spawn('App.exe');

Upvotes: 2

Related Questions