简体   繁体   English

如果执行exe,则节点JS exec不会回调

[英]Node JS exec doesn't callback if exec an exe

I have the following exec command in a Node JS application that launches an EXE: 我在启动EXE的Node JS应用程序中具有以下exec命令:

var exec = require('child_process').exec;

var theApp = 'HelloWorld';

var theCommand = 'C:/Program Files/' + theApp + '/dist/' + theApp + '-win32/' + theApp + '.exe';

exec(theCommand, function(error, stdout, stderr) {
    console.log('command callback');
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);
    }
});

The EXE is launched fine, but none of the console logs are fired inside of the exec command, so it's as though calling an exe doesn't cause a callback to be fired. EXE可以很好地启动,但是在exec命令中不会触发任何控制台日志,因此好像调用exe不会导致触发回调。 If I exec another Node app, eg node app.js then it fires the callback! 如果我执行另一个Node应用程序,例如node app.js那么它将触发回调! So it's because I'm calling an EXE to be opened! 这是因为我要打开一个EXE!

How can I solve this? 我该如何解决?

When the program you are running starts up and does not terminate, you will not get any sort of callback or event until the program eventually exits. 当您正在运行的程序启动并没有终止时,您将不会获得任何回调或事件,直到该程序最终退出。 The system simply does not define any sort of event for that condition. 系统只是没有为该条件定义任何类型的事件。 A child process is either running or not. 子进程正在运行或没有运行。 For any further detail about its condition, you are expected to communicate with it in some way (stdin, stdout, stderr, connect socket to it, interrogate the process in the system, etc...) 有关其状况的任何进一步详细信息,您都应该以某种方式与它进行通信(stdin,stdout,stderr,将套接字连接到它,询问系统中的进程等)。

Since the program can literally be doing anything, all you can know from the outside is whether it exited quickly with an error or exited quickly with no error or whether it appears to be still running. 由于该程序实际上可以执行任何操作,因此您从外面可以知道的是它是错误退出还是没有错误退出还是看上去仍在运行。 The return value from the exec() call contains a process ID so you can also query some info about that process ID if there's something specifically you want to know. exec()调用的返回值包含一个进程ID,因此,如果您想特别了解某些信息,您还可以查询有关该进程ID的一些信息。

Here's an example of what you could do: 这是您可以做什么的示例:

var exec = require('child_process').exec;

var theCommand = "notepad sample.txt";

function runit(cmd, timeout) {

    return new Promise(function(resolve, reject) {
        var ch = exec(theCommand, function(error, stdout, stderr) {
            if (error) {
                reject(error);
            } else {
                resolve("program exited without an error");
            }
        });
        setTimeout(function() {
            resolve("program still running");
        }, timeout);
    });
}

runit(theCommand, 1000).then(function(data) {
    console.log("success: ", data);
}, function(err) {
    console.log("fail: ", err);
});

It isn't clear to me which way you want it to act if the program you're running exits quickly, but without an error (the first call to resolve() in the code). 我不清楚,如果正在运行的程序迅速退出,但希望没有错误(代码中第一次调用resolve() ,您希望它以哪种方式起作用。 You could change that to a reject() depending upon behavior what you want. 您可以根据所需的行为将其更改为reject() I assumed that an exit without an error was not an error, but your situation might be different. 我以为没有错误的退出不是错误,但是您的情况可能有所不同。

Note: if you aren't actually waiting for the completion of the other program, you may not want to use .exec() since that is part of what it is built for. 注意:如果您实际上不是在等待其他程序的完成,则可能不希望使用.exec()因为这是它构建的一部分。 You may want to use one of the other child process creation methods. 您可能要使用其他子进程创建方法之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM