电子开放&保持打开cmd.exe

时间:2016-12-05 13:43:35

标签: windows electron

我正在构建一个小工具,除此之外我还可以启动一个Tomcat服务器。

非常简单,我只需要一个按钮即可启动startup.bat,另一个按钮可以调用shutdown.bat

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

我知道我可以得到溪流,但我不想打扰它。

此外,我只是在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);
        }
    })
});

1 个答案:

答案 0 :(得分:0)

最后,我还没有充分阅读文档,有一个名为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 
     }
);
相关问题