Spawn a process in electron


#1

I have seen some posts about the topic,yes,it run very well when I used electron command in console.But it couldn’t work when I packaged(electron-builder) the electron app,and appeared a problem when I started the app: it was starting create new process constantly,therefore there were a lot of processes in task manager we can see and CPU usage came to 100%. I don’t know how dose this happened, could you help me? please! Here is my test code:

master.js(main):

var childProcess = require('child_process');
let createProc = () => {
  let sp = childProcess.spawn(process.execPath, ['./go.js'], {stdio:'ignore'});
  sp.unref();
  sp.on('error', (err) => {
    console.log('failed to start process',err);
  });
  sp.on('exit',(code, signal) => {
    console.log(`child process exited with code ${code}`);
    createProc();
  });
}

createProc();

go.js:

const {app, BrowserWindow} = require('electron');
let basicWindowParams = {
  show: true,
  autoHideMenuBar:true
};
console.log(process.execPath)
console.log(process.execArgv)
console.log(process.pid)
app.on('ready', () => {
  let win = new BrowserWindow(basicWindowParams);
  win.loadURL('http://www.reddit.com/');
  win.on('did-finish-load', () => {
    win.show();
  });
});

OS: Windows 10

Electron: 1.8.1


#2

from what i can guess, referring to this code

it looks like you are spawning your own app process ( let’s say your app named “xzycn.exe” ).
so, it will create the process as this command “xzycn.exe go.js”.

it works on dev environment the exe path is electron.exe which can accept go.js as its parameter.

I think what you need to do is to handle the parameter of your app to accept go.js
, then it skips all logic of master.js.


#3

@anurocha Thank you for your reply. However, it still get the same issue when I specific a electron path at the position that process.execPath at. :frowning:


#4

it will be more easier for you if you build 2 apps instead of one.
however, you can try below code which i modified to merged 2 files to single one. but I handle the entry point by commandline parameter switch. you need to package it before run.

if(process.argv[1] == '--go' || process.argv[2] == '--go'){
  const {app, BrowserWindow} = require('electron');
  let basicWindowParams = {
    show: true,
    autoHideMenuBar:true
  };
  console.log(process.execPath)
  console.log(process.execArgv)
  console.log(process.pid)
  app.on('ready', () => {
    let win = new BrowserWindow(basicWindowParams);
    win.loadURL('http://www.reddit.com/');
    win.on('did-finish-load', () => {
      win.show();
    });
  });
  app.on('window-all-closed', () => {
    app.quit()
  })
}
else {
  var childProcess = require('child_process');
  let createProc = () => {
      console.log(process.execPath);
    let sp = childProcess.spawn(process.execPath, ['--go'], {stdio:'ignore'});
    sp.unref();
    sp.on('error', (err) => {
      console.log('failed to start process',err);
    });
    sp.on('exit',(code, signal) => {
      console.log(`child process exited with code ${code}`);
      createProc();
    });
  }

  createProc();
}