修改"..." | tee -a out.txt"流输出直播,而不是完成? [英] Modifying "... | tee -a out.txt" to stream output live, rather than on completion?

查看:641
本文介绍了修改"..." | tee -a out.txt"流输出直播,而不是完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在文件上输出命令的输出.假设我的命令是zip -r zip.zip directory,我需要向文件中添加/写入(这些选项中的任何一个都可以).到目前为止,我已经zip zip.zip directory | tee -a out.txt了,但是它似乎不起作用,它只是在命令结束后才写入整个输出...我该如何实现呢?

I would need to output the output of a command on a file. Let's say my command is zip -r zip.zip directory , I would need to append/write (any of these options would be fine) to a file (let's say out.txt). I got zip zip.zip directory | tee -a out.txt so far, but it doesn't seem to work, it just writes the whole output when the command is over... How can I achieve this?

谢谢;)

推荐答案

背景(例如,为什么?)

立即重定向-运行somecommand | tee -a out.txt时,设置somecommand并将其stdout直接发送 tee命令,该命令由其文档定义为未缓冲,从而将其输入上的所有可用内容尽快写入其指定的输出接收器.同样,somecommand >out.txtsomecommand设置为甚至在 开始之前直接写入out.txt.

Background (ie. Why?)

Redirections are immediate -- when you run somecommand | tee -a out.txt, somecommand is set up with its stdout sent directly to a tee command, which is defined by its documentation to be unbuffered, and thus to write anything available on its input to its specified output sinks as quickly as possible. Similarly, somecommand >out.txt sets somecommand to be writing to out.txt literally before it's even started.

立即是刷新缓冲的输出.

What's not immediate is flushing of buffered output.

这就是说:标准C库和大多数其他工具/语言在stdout上输出 buffer ,将小写合并为大写.这通常是理想的,因为减少了往返内核空间(上下文切换")的调用次数,而有利于执行更小数量的更有效,更大的写操作.

That is to say: The standard C library, and most other tools/languages, buffer output on stdout, combining small writes into big ones. This is generally desirable, inasmuch as decreases the number of calls to and from kernel space ("context switches") in favor of doing a smaller number of more efficient, larger writes.

因此,您的程序并没有真正等待直到退出以写入其输出-而是等待它们的缓冲区(可能为32kb或64kb,或其他)充满.如果它根本不会产生那么多输出,那么只有在关闭输出流时才会刷新它.

So your program isn't really waiting until it exits to write its output -- but it is waiting until its buffer (of maybe 32kb, or 64kb, or whatever) is full. If it never generates that much output at all, then it only gets flushed when closing the output stream.

如果您使用的是GNU平台,并且程序将文件描述符保留为找到它们的方式,而不是尝试显式配置缓冲 ,则可以使用stdbuf命令进行配置像这样缓冲:

If you're on a GNU platform, and your program is leaving its file descriptors the way it found them rather than trying to configure buffering explicitly, you can use the stdbuf command to configure buffering like so:

stdbuf -oL somecommand | tee -a out.txt

在运行somecommand时将stdout(-o)定义为行缓冲(L).

defines stdout (-o) to be line-buffered (L) when running somecommand.

或者,如果已安装expect,则可以使用它包括的unbuffer帮助器:

Alternately, if you have expect installed, you can use the unbuffer helper it includes:

unbuffer somecommand | tee -a out.txt

...实际上将模拟TTY(如expect一样),获得与somecommand直接连接到控制台时相同的非缓冲行为.

...which will actually simulate a TTY (as expect does), getting the same non-buffered behavior you have when somecommand is connected directly to a console.

这篇关于修改"..." | tee -a out.txt"流输出直播,而不是完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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