电子杀死child_process.exec [英] Electron kill child_process.exec

查看:43
本文介绍了电子杀死child_process.exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子应用程序,该应用程序使用 child_process.exec 来运行长时间运行的任务.我正在努力管理在这些任务期间用户何时退出应用程序.

如果它们退出我的应用程序或关闭,则子进程将继续运行直到完成,但是电子应用程序窗口已关闭并退出.

是否可以通知用户仍有进程在运行,等他们完成后再关闭应用程序窗口?

我的 main.js 中只有标准代码:

 //关闭所有窗口时退出.app.on('window-all-closed',function(){//在OS X上,这对于应用程序及其菜单栏很常见//保持活动状态,直到用户使用Cmd + Q明确退出为止如果(process.platform!='darwin'){app.quit();}}); 

我应该在某处添加支票吗?

感谢您的帮助

已编辑

在完成之前,我似乎无法获取 child_process 的PID.这是我的 child_process 代码

  var loader = child_process.exec(cmd,function(error,stdout,stderr){console.log(loader.pid)如果(错误){console.log(error.message);}console.log('Loaded:',value);}); 

我应该尝试以其他方式获得它吗?

解决方案

因此,在每个人都发表了精彩的评论之后,我能够添加一些附加功能来更新我的代码以使其正常工作,所以我将更新发布给其他人./p>

1)从child_process.exec更改为child_process.spawn

var loader = child_process.spawn('program',options,{detached:true})

2)使用Electron ipcRenderer从我的模块与main.js脚本进行通信.这使我可以将PID发送到main.js

ipcRenderer.send('pid-message',loader.pid);

  ipcMain.on('pid-message',function(event,arg){console.log('Main:',arg);pids.push(arg);}); 

3)将这些PID添加到数组

4)在我的main.js中,添加了以下代码以在退出应用程序之前杀死数组中存在的所有PID.

 //应用程序关闭处理程序app.on('退出前',function(){pids.forEach(function(pid){//一个简单的pid查找ps.kill(pid,function(err){如果(错误){抛出新的Error(err);}别的 {console.log('进程%s已被杀死!',pid);}});});}); 

感谢大家的帮助.

I have an electron app that uses child_process.exec to run long running tasks. I am struggling to manage when the user exits the app during those tasks.

If they exit my app or hit close the child processes continue to run until they finish however the electron app window has already closed and exited.

Is there a way to notify the user that there are process still running and when they have finished then close the app window?

All I have in my main.js is the standard code:

// Quit when all windows are closed.
app.on('window-all-closed', function() {
    // On OS X it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform != 'darwin') {
        app.quit();
    }
});

Should I be adding a check somewhere?

Thanks for your help

EDITED

I cannot seem to get the PID of the child_process until it has finished. This is my child_process code

var loader = child_process.exec(cmd, function(error, stdout, stderr) {
    console.log(loader.pid)
    if (error) {
        console.log(error.message);
    }
    console.log('Loaded: ', value);
});

Should I be trying to get it in a different way?

解决方案

So after everyones great comments I was able to update my code with a number of additions to get it to work, so am posting my updates for everyone else.

1) Change from child_process.exec to child_process.spawn

var loader = child_process.spawn('program', options, { detached: true })

2) Use the Electron ipcRenderer to communicate from my module to the main.js script. This allows me to send the PIDs to main.js

ipcRenderer.send('pid-message', loader.pid);

ipcMain.on('pid-message', function(event, arg) {
  console.log('Main:', arg);
  pids.push(arg);
});

3) Add those PIDs to array

4) In my main.js I added the following code to kill any PIDs that exist in the array before exiting the app.

// App close handler
app.on('before-quit', function() {
  pids.forEach(function(pid) {
    // A simple pid lookup
    ps.kill( pid, function( err ) {
        if (err) {
            throw new Error( err );
        }
        else {
            console.log( 'Process %s has been killed!', pid );
        }
    });
  });
});

Thanks for everyones help.

这篇关于电子杀死child_process.exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆