我正在开发一个 NodeJs 应用程序,它可以帮助我公司的开发人员开发我们基于 Electron 的产品。它会进行一些自动化操作,最后会自动启动 Electron-app。
从 NodeJs 内部启动 Electron 应用程序不是问题。通常,应用程序以 bash 脚本启动,如下所示:
#!/bin/sh
HOME=$PWD/home-dir ./node_modules/.bin/electron myAppDir
myAppDir
是我的 Electron-App 的目录,也可以是一个 JavaScript 文件。
值得一提的是,./node_modules/.bin/electron
只是指向 ./node_modules/electron/cli.js
的符号链接(symbolic link)
我做了以下操作:
const app = execFile('/the/path/to/the/bash/script', [], {
windowsHide: true,
},(error, stdout, stderr) => {
if (error) {
throw error;
}
warn('The app was terminated');
});
这会很好地启动应用程序。但是,如果我执行 app.kill('SIGTERM');
,它会输出 'The app was terminated' 但应用程序本身不会关闭。
我尝试改为执行 node_modules/.bin/electron
或 ./node_modules/electron/cli.js
:
const app = execFile('/the/path/to/node_modules/.bin/electron', ['myAppDir'], {
windowsHide: true,
detached: true,
env: {
HOME: 'path/to/home'),
}
我可以再次启动 Electron 应用程序 - 当我执行 app.kill('SIGTERM');
编辑:
我的假设是, Electron 启动器实际上产生了一个新的子进程,因此终止启动器不会停止实际启动的应用程序。
这是./node_modules/.bin/electron
的内容(或者分别是./node_modules/electron/cli.js
)
#!/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)
})
最佳答案
您可以在 Node 应用程序中使用此库 https://www.npmjs.com/package/systeminformation并使用其 .processes(cb) 方法找到正在运行的 Electron 应用程序并在 processes 方法的回调中将其终止。
关于javascript - 如何从 NodeJs 启动和停止 Electron 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52258086/