如何在父进程被终止的情况下保持子进程的活力

问题描述 投票:1回答:2

我有一个Electron应用程序,我必须启动一个exe文件并退出应用程序。问题是当我试图退出时,应用程序关闭,exe文件也在之前启动......。我想让我的exe文件被打开。

我的代码是这样的

   var execFile = require('child_process').execFile;
   execFile(filePath).unref();

   const { remote }  = require('electron');
   const { app } = remote;
   app.quit();

如何在app.quit()之后打开我的文件并保持其活力?

谢谢你

javascript node.js cordova electron execfile
2个回答
0
投票

我们可以使用detached选项在后台执行。

const { spawn } = require('child_process');

const child = spawn(`exefile path`, [...args], {
  detached: true,
});

child.unref();

分离子进程的具体行为取决于操作系统。在Windows上,分离的子进程将有自己的控制台窗口,而在Linux上,分离的子进程将成为一个新进程组和会话的领导。

如果在分离的进程上调用unref函数,父进程可以独立于子进程退出。如果子进程正在执行一个长期运行的进程,这可能会很有用,但为了让它在后台运行,子进程的stdio配置也必须独立于父进程。


0
投票

我使用 spawn 解决了这个问题

var execFile = require('child_process').spawn;
execFile(filePath, [], {'detached':true});

const { remote }  = require('electron');
const { app } = remote;
app.quit();
© www.soinside.com 2019 - 2024. All rights reserved.