Daniele
Daniele

Reputation: 41

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

Upvotes: 2

Views: 1762

Answers (2)

Daniele
Daniele

Reputation: 41

I solved this using spawn

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

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

Upvotes: 0

tpikachu
tpikachu

Reputation: 4854

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.

Upvotes: 1

Related Questions