Martin Medro
Martin Medro

Reputation: 147

How to run Electron app independent of terminal?

I am looking for a way to run an Electron app (npm startcommand) independently of the terminal itself. Meaning that I expect the Electron app to keep running even if the terminal closes..

I am not sure whether it is possible.

I have tried cd electron-directory-path && nohup npm start &, but this though allows me to use the terminal instance for other commands and prevents any electron messages from popping up in the terminal. But, closing the terminal still kills the Electron app.

Even cd electron-directory-path && npm start & does the same thing, but I haven't yet been able to find a way to run the Electron app completely independent of the terminal instance...

Upvotes: 2

Views: 1959

Answers (3)

S.Serpooshan
S.Serpooshan

Reputation: 7440

- Method 1)

Create a command file (.bat) as:

  REM ensure we are in the path of our bat file:
  @cd /d %~dp0
  
  start "" node_modules\electron\dist\electron.exe .\main.js

this differ from npm start as it call electron.cmd which in turn call special file cli.js which tries to open electron app as attached to terminal window. But the above code calls electron.exe file directly to run the app.

- Method 2)

add following file (named run.js) to your app:

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

// Adjust the path to your Electron executable file
let electronPath = "node_modules\\electron\\dist\\electron.exe"; //if you use local copy of electron/node modules in your app root folder
//let electronPath = process.env.APPDATA + '\\npm\\node_modules\\electron\\dist\\electron.exe'; //if you install electron as global package (npm install -g electron) 
 
let appPath = 'main.js';

// Spawn the Electron process
const electronProcess = spawn(electronPath, [appPath], {detached: true, stdio: 'ignore'});

// Unref to allow the parent process to exit independently
electronProcess.unref();

Now, executing node run.js is enough to start the app. So, we can create a New Shortcut, set its Target to node run.js, then edit its properties and ensure its Start in path is same as your app root folder (not node path). Open this Shortcut to start the app without any command windows open while running the app.

Upvotes: 0

Martin Medro
Martin Medro

Reputation: 147

Let pathname be the path to the node app location. Just use the command:

cd pathname && npm start && ^Z &


  • cd to change directory to where we need to execute the terminal command.
  • && to mean there are other commands to be executed after this one.
  • npm start to start npm app
  • ^Z to suspend the process running in the terminal, and hence disconnect the terminal part of node from the original app.
  • & to mean that we don't want the terminal to wait for the command to execute.

Now we can close the terminal, and the electron app should keep running...!


Credits:

Upvotes: 0

snwflk
snwflk

Reputation: 3517

You start an Electron app through nohup npm start &, but when closing the terminal window, the Electron app also terminates (against expectation).

I can reproduce the behavior, but not all the times. In roughly 30% of my experiments, the Electron app was not terminated. I was not able to find the reason for this varying behavior yet.

Workaround

The following workaround closes the terminal without terminating the Electron app. In my tests, it has worked every time:

  • Start the Electron app as before: nohup npm start &

  • Close the running terminal by issuing nohup kill $$ &

    The $$ gives the current process id. Note that kill $$ doesn't work.


If you don't necessarily need to run from a terminal, you can also create a desktop file to start the app.

Upvotes: 2

Related Questions