[英]Modifying “… | tee -a out.txt” to stream output live, rather than on completion?
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
).假设我的命令是
zip -r zip.zip directory
,我需要将(这些选项中的任何一个都可以)附加/写入文件(比如说out.txt
)。 I got zip zip.zip directory | tee -a out.txt
我得到了
zip zip.zip directory | tee -a out.txt
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? zip zip.zip directory | tee -a out.txt
到目前为止,但它似乎不起作用,它只是在命令结束时写入整个输出......我怎样才能做到这一点?
Thanks ;)谢谢 ;)
Redirections are immediate -- when you run somecommand | tee -a out.txt
重定向是即时的——当你运行
somecommand | tee -a out.txt
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. somecommand | tee -a out.txt
, somecommand
设置为将其标准输出直接发送到tee
命令,该命令由其文档定义为无缓冲,从而尽快将其输入上可用的任何内容写入其指定的输出接收器。 Similarly, somecommand >out.txt
sets somecommand
to be writing to out.txt
literally before it's even started.类似地,
somecommand >out.txt
将somecommand
设置为在out.txt
开始之前写入。
What's not immediate is flushing of buffered output.不是立即刷新缓冲输出。
That is to say: The standard C library, and most other tools/languages, buffer output on stdout, combining small writes into big ones.也就是说:标准 C 库和大多数其他工具/语言在 stdout 上缓冲输出,将小写组合成大写。 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.
这通常是可取的,因为减少进出内核空间(“上下文切换”)的调用次数,有利于进行更少量、更高效、更大的写入。
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.所以,你的程序是不是真的等到它退出来写它的输出-但它正在等待(可能的32KB,64KB或,或其他),直到它的缓冲区已满。 If it never generates that much output at all, then it only gets flushed when closing the output stream.
如果它从不产生那么多输出,那么它只会在关闭输出流时被刷新。
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:如果您在 GNU 平台上,并且您的程序将其文件描述符保留为找到它们的方式,而不是尝试显式配置缓冲,则可以使用
stdbuf
命令来配置缓冲,如下所示:
stdbuf -oL somecommand | tee -a out.txt
defines stdout ( -o
) to be line-buffered ( L
) when running somecommand
.在运行
somecommand
时将 stdout ( -o
) 定义为行缓冲 ( L
)。
Alternately, if you have expect
installed, you can use the unbuffer
helper it includes:或者,如果您已经安装了
expect
,您可以使用它包含的unbuffer
帮助程序:
unbuffer somecommand | tee -a out.txt
...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. ...它实际上会模拟 TTY(如
expect
那样),获得与somecommand
直接连接到控制台时相同的非缓冲行为。
您是否尝试过 option command > out.log 2>&1
this log 将所有内容归档而不显示任何内容,所有内容都将直接进入该文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.