node.js 使用 child_process.execSync 但将输出保留在控制台中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30134236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:44:47  来源:igfitidea点击:

Use child_process.execSync but keep output in console

node.jschild-process

提问by suamikim

I'd like to use the execSyncmethod which was added in NodeJS 0.12 but still have the output in the console window from which i ran the Node script.

我想使用execSync在 NodeJS 0.12 中添加的方法,但在控制台窗口中仍然有输出,我从中运行 Node 脚本。

E.g. if I run a NodeJS script which has the following line I'd like to see the full output of the rsync command "live" inside the console:

例如,如果我运行具有以下行的 NodeJS 脚本,我希望在控制台内看到 rsync 命令“live”的完整输出:

require('child_process').execSync('rsync -avAXz --info=progress2 "/src" "/dest"');

I understand that execSyncreturns the ouput of the command and that I could print that to the console after execution but this way I don't have "live" output...

我知道execSync返回命令的输出,并且我可以在执行后将其打印到控制台,但这样我就没有“实时”输出......

回答by gregers

You can pass the parent′s stdio to the child processif that′s what you want:

如果这是你想要的,你可以将父进程的 stdio 传递给子进程

require('child_process').execSync(
    'rsync -avAXz --info=progress2 "/src" "/dest"',
    {stdio: 'inherit'}
);

回答by Ethan

You can simply use .toString().

您可以简单地使用.toString().

var result = require('child_process').execSync('rsync -avAXz --info=progress2 "/src" "/dest"').toString();
console.log(result);

This has been tested on Node v8.5.0, I'm not sure about previous versions. According to @etov, it doesn't work on v6.3.1- I'm not sure about in-between.

这已经在 Node 上进行了测试v8.5.0,我不确定以前的版本。根据@etov 的说法,它不起作用v6.3.1- 我不确定介于两者之间。

回答by Brian

Unless you redirect stdout and stderr as the accepted answer suggests, this is not possible with execSync or spawnSync. Without redirecting stdout and stderr those commands only return stdout and stderr when the command is completed.

除非您按照接受的答案建议重定向 stdout 和 stderr,否则这对于 execSync 或 spawnSync 是不可能的。如果不重定向 stdout 和 stderr,这些命令仅在命令完成时返回 stdout 和 stderr。

To do this without redirecting stdout and stderr, you are going to need to use spawn to do this but it's pretty straight forward:

要在不重定向 stdout 和 stderr 的情况下执行此操作,您将需要使用 spawn 来执行此操作,但它非常简单:

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

//kick off process of listing files
var child = spawn('ls', ['-l', '/']);

//spit stdout to screen
child.stdout.on('data', function (data) {   process.stdout.write(data.toString());  });

//spit stderr to screen
child.stderr.on('data', function (data) {   process.stdout.write(data.toString());  });

child.on('close', function (code) { 
    console.log("Finished with code " + code);
});

I used an ls command that recursively lists files so that you can test it quickly. Spawn takes as first argument the executable name you are trying to run and as it's second argument it takes an array of strings representing each parameter you want to pass to that executable.

我使用了一个 ls 命令递归地列出文件,以便您可以快速测试它。Spawn 将您尝试运行的可执行文件名称作为第一个参数,作为第二个参数,它需要一个字符串数组,表示您要传递给该可执行文件的每个参数。

However, if you are set on using execSync and can't redirect stdout or stderr for some reason, you can open up another terminal like xterm and pass it a command like so:

但是,如果您设置为使用 execSync 并且由于某种原因无法重定向 stdout 或 stderr,则可以打开另一个终端(如 xterm)并向其传递如下命令:

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

execSync("xterm -title RecursiveFileListing -e ls -latkR /");

This will allow you to see what your command is doing in the new terminal but still have the synchronous call.

这将允许您查看您的命令在新终端中执行的操作,但仍然具有同步调用。