bash 将 iostat 输出重定向到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22071817/
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
Redirecting iostat output to a file
提问by isaac.hazan
Why the output of iostat is not redirected to the file in the following command:
为什么 iostat 的输出没有重定向到以下命令中的文件:
iostat -x 3 | awk '/sda/ { print , }' > /tmp/disk_utilization
Is iostat behaving differently than any other command?
iostat 的行为是否与任何其他命令不同?
Thx in advance
提前谢谢
回答by n0741337
Looks like you need to add a count to tell the stream when to terminate( man iostat). You're only adding the interval:
看起来您需要添加一个计数来告诉流何时终止(man iostat)。您只添加了间隔:
If the interval parameter is specified without the count parameter, the iostat command generates reports continuously.
When I try:
当我尝试:
iostat -x 3 6 | awk '/sda/ { print , }' > outfile
the 6
tells iostat
to stop after 6 iterations of 3 second intervals. After the iterations iostat
completes and I get an output
file.
的6
讲述iostat
后3秒的时间间隔6次迭代停止。迭代iostat
完成后,我得到一个output
文件。
Alternatively, you could pick some expression to cause the awk script to exit. When I try:
或者,您可以选择一些表达式来导致 awk 脚本退出。当我尝试:
iostat -x 3 | awk -v max=10 '/sda/ { print , ; max++ } NR > max { exit } ' > outfile2
I get just 3 lines in output2
which makes sense because iostat -x
produces about a screens worth of lines of output to the screen.
我只得到 3 行,output2
这是有道理的,因为iostat -x
产生了大约一个屏幕的屏幕输出行。
You could also force the awk to fflush(stdout)
in each print cycle:
您还可以fflush(stdout)
在每个打印周期中强制 awk :
iostat -x 3 | awk '/sda/ { print , ; fflush(stdout) }' > output3
and then close iostat
with a <ctrl+c>
from the shell.
然后iostat
用一个<ctrl+c>
从外壳关闭。
回答by Adrian Frühwirth
The cause of your problem is that the pipe is buffered (probably 4k
), so if you wait long enough for the output to be flushed to the file it will no longer be empty.
您的问题的原因是管道被缓冲(可能4k
),因此如果您等待足够长的时间将输出刷新到文件中,它将不再为空。