Node.js:捕获child_process.spawn的STDOUT [英] Node.js: Capture STDOUT of `child_process.spawn`

查看:126
本文介绍了Node.js:捕获child_process.spawn的STDOUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要捕获生成的子进程的自定义输出.

I need to capture in a custom stream outputs of a spawned child process.

child_process.spawn(command[, args][, options])

例如,

var s = fs.createWriteStream('/tmp/test.txt');
child_process.spawn('ifconfig', [], {stdio: [null, s, null]})

现在如何实时读取/tmp/test.txt ?

似乎 child_process.spawn 并未使用 stream.Writable.prototype.write stream.Writable.prototype._write 执行.

例如,

s.write = function() { console.log("this will never get printed"); };

以及

s.__proto__._write = function() { console.log("this will never get printed"); };

似乎它使用文件描述符幕后 child_process.spawn 写入文件.

It looks like it uses file descriptors under-the-hood to write from child_process.spawn to a file.

这样做不起作用:

var s2 = fs.createReadStream('/tmp/test.txt');
s2.on("data", function() { console.log("this will never get printed either"); });

那么,如何获得子进程的 STDOUT 内容?

So, how can I get the STDOUT contents of a child process?

我想要实现的是将子进程的 STDOUT 流式传输到套接字.如果我将套接字作为 stdio 参数直接提供给 child_process.spawn ,则套接字在完成时会关闭,但我想保持套接字打开.

What I want to achieve is to stream STDOUT of a child process to a socket. If I provide the socket directly to the child_process.spawn as a stdio parameter it closes the socket when it finishes, but I want to keep it open.

更新:

解决方案是使用默认的 {stdio:['pipe','pipe','pipe']} 选项,并听从创建的 .stdout 子进程.

The solution is to use default {stdio: ['pipe', 'pipe', 'pipe']} options and listen to the created .stdout of the child process.

var cmd = child_process.spaw('ifconfig');
cmd.stdout.on("data", (data) => { ... });

现在,要解决这个问题,还有一个更具挑战性的问题:

Now, to up the ante, a more challenging question:

-您如何阅读子进程的 STDOUT 并仍然保留颜色?

-- How do you read the STDOUT of the child process and still preserve the colors?

例如,如果您像这样将 STDOUT 发送到 process.stdout :

For example, if you send STDOUT to process.stdout like so:

child_process.spawn('ifconfig', [], {stdio: [null, process.stdout, null]});

它将保留颜色并将彩色输出打印到控制台,因为 .isTTY 属性在 process.stdout 上设置为 true

it will keep the colors and print colored output to the console, because the .isTTY property is set to true on process.stdout.

process.stdout.isTTY // true

现在,如果您使用默认的 {stdio:['pipe','pipe','pipe']} ,将读取的数据将去除控制台颜色.您如何获得颜色?

Now if you use the default {stdio: ['pipe', 'pipe', 'pipe']}, the data you will read will be stripped of console colors. How do you get the colors?

一种方法是使用 fs.createWriteStream 创建自己的自定义流,因为 child_process.spawn 要求您的流具有文件描述符.

One way to do that would be creating your own custom stream with fs.createWriteStream, because child_process.spawn requires your streams to have a file descriptor.

然后将该流的 .isTTY 设置为 true ,以保留颜色.

Then setting .isTTY of that stream to true, to preserve colors.

最后,您将需要捕获 child_process.spawn 写入该流的数据,但是由于 child_process.spawn 不使用 .prototype.write或流的 .prototype._write ,您需要以其他 hacky 方式捕获其内容.

And finally you would need to capture the data what child_process.spawn writes to that stream, but since child_process.spawn does not use .prototype.write nor .prototype._write of the stream, you would need to capture its contents in some other hacky way.

这可能就是为什么 child_process.spawn 要求您的流具有文件描述符的原因,因为它绕过了 .prototype.write 调用并直接写入以下文件-引擎盖.

That's probably why child_process.spawn requires your stream to have a file descriptor because it bypasses the .prototype.write call and writes directly to the file under-the-hood.

有什么想法要实现吗?

推荐答案

您可以在不使用临时文件的情况下进行操作:

You can do it without using a temporary file:

var process = child_process.spawn(command[, args][, options]);
process.stdout.on('data', function (chunk) {
    console.log(chunk);
});

这篇关于Node.js:捕获child_process.spawn的STDOUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆