[英]How to get the output from NodeJS when use the child_process.spawn to launch a bat on windows OS, and the bat will open a new terminal
If the bat only run one terminal ,we can got the stdout, but we will failded if this open a new window. 如果蝙蝠只运行一个终端,我们可以得到标准输出,但是如果打开一个新窗口,我们将失败。
var terminal = require('child_process').spawn('aa.bat');
console.log('Starting..terminal.pid.', terminal.pid, "process.pid", process.pid);
terminal.stdout.on('data', function(data) {
console.log('stdout:',data);
});
terminal.stderr.on('data', function(data) {
console.log('stderr:',data);
});
terminal.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});
terminal.on('exit', function(code) {
console.log('exit code:', code, ' terinal.pid.', terminal.pid, "process.pid", process.pid);
console.log('child process', process.pid, 'exited with code ' + code);
});
presumed the bat file like this 假设这样的蝙蝠文件
start cmd
if we change it to 如果我们将其更改为
start /b cmd
this will not open a new terminal ,the nodeJs will work 这将不会打开新的终端,nodeJs将起作用
It's difficult to get another process stdout from NodeJS on windows os, at most time it open an new terminal window. 在Windows操作系统上很难从NodeJS获得另一个进程标准输出,大多数时候它会打开一个新的终端窗口。 but you can use an bash to launch it. 但您可以使用bash启动它。
1.The bash.exe you can download from http://www.steve.org.uk/Software/bash/ then the code maybe like this: 1.您可以从http://www.steve.org.uk/Software/bash/下载bash.exe,然后代码可能像这样:
var terminal = require('child_process');
function start() {
if (process.platform.trim() !== 'win32') {
terminal = terminal.spawn('bash');
console.log('This is not the win32 plantform ,please confirm the bash woking!');
} else {
terminal = terminal.spawn('./bash-2.03/bash.exe');
}
// !!!must append the ./
//terminal.stdin.write('./aa.exe');
terminal.stdin.write('./aa.bat');
terminal.stdin.end();
};
start();
terminal.stdout.on('data', function(data) {
console.log(data + "");
});
terminal.stdout.on('error', function(data) {
console.log('error:\n' + data);
});
terminal.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});
terminal.on('exit', function(code) {
console.log('exit code:', code, ' terinal.pid.', terminal.pid, "process.pid", process.pid);
console.log('child process', process.pid, 'exited with code ' + code);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.