简体   繁体   中英

Electron js child process not getting killed

I'am writing a electron js script to run an.exe file. Idea is when i click on button 'start'.exe should start as child process and when i click on 'stop' the child process should get killed.

I'am using IPC for communication.

const getScriptPath = () =>{
  if(process.platform==='win32'){
    return path.join(__dirname, 'dist_folder','pydoc.exe')

  }
}

const createPyProc =() =>{
  let script = getScriptPath()
  pyProc = require('child_process').execFile(script)  
  allProcess.push(pyProc)  

  }

}

const exitPyProc=() => {

    allProcess.forEach(function(proc){
      proc.kill();
    });


}
ipc.on('start_script',function(event){
  createPyProc()

})

ipc.on('stop_script', function(event){
  exitPyProc()

})

when i click on button start i can see in task Manager child process starts under electron main process and it is get killed after pressing kill button.

problem: 1. Still a residual independent process is left in task manager of pydoc.exe even i close electron window where child process under electron already killed.

Is my child process command is correct?

 pyProc = require('child_process').execFile(script)  
  const subprocess = spawn(getScriptPath(), args);

  subprocess.stdout.on('data', data => {
    console.log(`Daemon stdout: ${data}`);
    resolve(data.toString());
    // Here is where the output goes
  });
  subprocess.stderr.on('data', data => {
    console.log(`Daemon stderr: ${data}`);
    resolve(data.toString());
    // Here is where the error output goes
  });
  subprocess.on('close', code => {
    console.log(`Successfully closed. ${code}`);
    // Here you can get the exit code of the script
  });

  ipc.on('stop_script', function(event){
    subprocess.kill(); 
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM