![](/img/trans.png)
[英]Child process not killed - Node js ( inside Electron js app, windows)
[英]Electron js child process not getting killed
我正在编写一个 electron js 脚本来运行一个 .exe 文件。 想法是当我单击“开始”按钮时。exe 应该作为子进程启动,当我单击“停止”时,子进程应该被杀死。
我正在使用 IPC 进行通信。
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()
})
当我单击按钮开始时,我可以在任务管理器中看到子进程在 electron 主进程下启动,并在按下终止按钮后被终止。
问题: 1. 即使我关闭 electron window 下 electron 下的子进程已经杀死,pydoc.exe 的任务管理器中仍然留下一个剩余的独立进程。
我的子进程命令是否正确?
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();
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.