简体   繁体   中英

How can keep child process alive even if parent process is terminated

I have an Electron app and I have to launch an exe file and quit the app. The problem is when I try to quit, app closes and exe file launched before too... I want my exe file opened instead...

This is my code

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

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

How can I open my file and keep it alive after app.quit()?

Thanks

We can execute it in the background using the detached option:

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

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

child.unref();

The exact behavior of detached child processes depends on the OS. On Windows, the detached child process will have its own console window while on Linux the detached child process will be made the leader of a new process group and session.

If the unref function is called on the detached process, the parent process can exit independently of the child. This can be useful if the child is executing a long-running process, but to keep it running in the background the child's stdio configurations also have to be independent of the parent.

I solved this using spawn

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

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

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