I am working in nodejs. I'm trying to run .cmd
file using child_process spawn and exec. But none seems to work. Spawn successfully run the .cmd
file when I run this script spawn('cmd', ['/c', 'start start_wMBusServices_CLI.cmd'])
.
but on killing the process using process.kill(pid)
it generates following error.
spawn.kill()
doesn't kill process at all.
I did some digging and I found that the process (pid = 16580) is missing in task manager.
And child_process.exec just create pid and doesn't run .cmd
at all.
Now, what I want is, to kill the process when I click that stop button.
I need a simple script to kill this process.
Here's my whole code
let exec = null;
let pId = null;
function startStop() {
try {
if (!pId) {
exec = spawn('cmd', ['/c', "start start_wMBusServices_CLI.cmd"])
// <-- execFile didn't work -->
-------------------------------
// exec = execFile(executablePath, { encoding: 'utf-8' }, function (err, stdOut, stdErr) {
// console.error(err);
// console.error(stdErr);
// })
pId = exec.pid
console.log(pId);
// if(exec.killed)
// pId = null
}
else if (pId) {
// didn't work
--------------
// exec.kill()
// generates error shown before
-------------------------------
process.kill(pId)
}
} catch (err) {
console.error(err);
}
console.log(process);
}