电子打开并保持打开 cmd.exe

Electron open & keep open cmd.exe

我正在构建一个小工具,它应该允许我启动 Tomcat 服务器。

很简单,我只需要一个按钮来启动 startup.bat 和另一个按钮来调用 shutdown.bat

它工作得很好(服务器启动和停止)但完全处于忍者模式,我无法设法获得带有日志的 Tomcat 控制台。 如果我调用 startup.bat,则从经典命令行打开 Window,其中包含日志。 我尝试了 execexecFilespawn。我试过直接调用batcmd.exe,甚至试过start。但是我无法得到 Window.

我知道我可以获得流,但我不想为此烦恼。

另外我只是在Windows上使用这个工具,暂时不用考虑其他平台。

const ipc = require('electron').ipcMain;
const execFile = require('child_process').execFile;

ipc.on('start-local-tomcat', function (event) {
    execFile('cmd.exe', ['D:\DEV\apache-tomcat-8.0.12\bin\startup.bat'],
    {env: {'CATALINA_HOME': 'D:\DEV\apache-tomcat-8.0.12'}},
    function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
        console.log('exec error: ' + error);
        }
    })
});

ipc.on('stop-local-tomcat', function (event) {
    execFile('cmd.exe',['D:\DEV\apache-tomcat-8.0.12\bin\shutdown.bat'],
    {env: {'CATALINA_HOME': 'D:\DEV\apache-tomcat-8.0.12'}},
    function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
        console.log('exec error: ' + error);
        }
    })
});

最后我只是没有充分阅读文档,有一个名为 detached 的参数可以完全满足我的要求:

var child = spawn(
     'D:\DEV\apache-tomcat-8.0.12\bin\startup.bat',
     {
         env: {'CATALINA_HOME': 'D:\DEV\apache-tomcat-8.0.12'},
        detached: true 
     }
);