4

I am working on a NodeJs application which helps the developers in my company with the development of our Electron-based product. It does some automatization and at the end, it starts the Electron-app automatically.

Starting the Electron app from inside NodeJs is not a problem. normally the apps are started with a bash script which looks like:

#!/bin/sh HOME=$PWD/home-dir ./node_modules/.bin/electron myAppDir 

myAppDir is the directory with my Electron-App, could be a JavaScript file as well.

Worth to mention, that ./node_modules/.bin/electron is just a symlink to ./node_modules/electron/cli.js

I did following:

const app = execFile('/the/path/to/the/bash/script', [], { windowsHide: true, },(error, stdout, stderr) => { if (error) { throw error; } warn('The app was terminated'); }); 

This starts the app just fine. However if I do app.kill('SIGTERM'); it outputs the 'The app was terminated' but the app itself does not close.

I tried to execute the node_modules/.bin/electron or the ./node_modules/electron/cli.js instead:

const app = execFile('/the/path/to/node_modules/.bin/electron', ['myAppDir'], { windowsHide: true, detached: true, env: { HOME: 'path/to/home'), } 

I can launch the Electron app but again - it does not close the running app when I do app.kill('SIGTERM');

EDIT:

My assumption is, that the electron launcher actually spawns a new subprocess, thus killing the launcher does not stops the actual launched app.

This is the content of ./node_modules/.bin/electron (or ./node_modules/electron/cli.js respectively)

#!/usr/bin/env node var electron = require('./') var proc = require('child_process') var child = proc.spawn(electron, process.argv.slice(2), {stdio: 'inherit'}) child.on('close', function (code) { process.exit(code) }) 
5
  • Did you try ‘app.quit()’? Commented Sep 10, 2018 at 12:55
  • Then I get TypeError: app.quit is not a function Commented Sep 10, 2018 at 13:20
  • 1
    You can do this from inside an electron app. I just start an electron app from an NodeJs script. Commented Sep 10, 2018 at 13:52
  • "I tried to execute the node_modules/.bin/electron" I couldn't with your code. The only thing worked for me was execFile('node', ['path/to/electron/cli.js', './myapp.js'] and it quits for kill successfully. Commented Sep 11, 2018 at 13:25
  • The ./myapp.js was the param1 Parameter in the original question, I edited my question so it is more obvious. Commented Sep 28, 2018 at 7:20

1 Answer 1

1

You can use this library in you node app https://www.npmjs.com/package/systeminformation and with its .processes(cb) method find running electron app and kill it in the callback of the processes method.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your suggestion, but I would accept this just as a workaround. It has to be possible to start and stop one NodeJs Application from within another NodeJs application without needing to deal with thePIDs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.