Kill a spawned process, on windows


#1

i’m starting a background process (on windows) from electron main, something like this:

app_exe = require("child_process").spawn(
  "app.exe" ,
  [ "--params", ... ],
  { stdio: "ignore" }
);

this works fine, i can see this from process explorer:

but i cannot kill the process when the electron is closed ( .on("closed") or on("window-all-closed") )

i tried child.kill([signal]), but also tree-kill or taskkill with no results: only the first process (6036 from the example) is killed, the second (5760) remains stale.

also child_process.exec taskkill /F /T /PID doesn’t kill it.

the only way to kill is exectaskkill /F /IM app.exe /T, but in this way i cannot run two instances of the electron app.

i’m missing something obvious on process management on windows?


#2

I did something like this to overcome this. Check if this works for you because I use exec instead of spawn

const psTree = require('ps-tree')
if(shell != null){
    psTree(shell.pid, function (err, children) {  // check if it works always
        children.map(function (p) {
            process.kill(p.PID);                    
        });
    });
}