将 pv 输出 (stderr) 打印到文件 [英] Print pv output (stderr) to file
问题描述
timeout 5s dd if=/dev/random |pv -r >/开发/空[ 505kiB/s]
速率线输出在我的五秒超时过程中更新".我试过了,但它不起作用(日志为空):
timeout 5s dd if=/dev/random |pv -r >/dev/null 2>费率日志
我相信它与stderr 输出,但一小时后我卡住了.理想情况下,我的日志文件每次都会有多行 pv打印一个新值:
[ 505kiB/s][ 498kiB/s][ 542kiB/s][ 513kiB/s][ 509kiB/s]
更新:
为了将内容放入我上面描述的文件中,我必须使用 stdbuf 虽然我不确定为什么需要它(tr 单独不起作用,如果没有 stdbuf):
timeout 5s dd if=/dev/random |pv -fr >/dev/null 2>>(stdbuf -oL tr '\r' '\n' >rates.log)
来自 man pv
:
- -f, --force
- 力输出.通常情况下,pv 不会输出任何视觉显示如果标准错误不是终端.此选项强制它这样做.
由于 rates.log
不是终端,您需要执行 pv -fr
而不是 pv -r
.
How can I print the stderr output of pv to a file? For example:
timeout 5s dd if=/dev/random | pv -r > /dev/null
[ 505kiB/s]
The rate line output is "updated" over the course of my five second timeout. I tried this but it does not work (log is empty):
timeout 5s dd if=/dev/random | pv -r > /dev/null 2> rates.log
I believe it has something to do with carriage returns in the stderr output, but after an hour I am stuck. Ideally my log file would have multiple lines each time pv prints a new value:
[ 505kiB/s]
[ 498kiB/s]
[ 542kiB/s]
[ 513kiB/s]
[ 509kiB/s]
UPDATE:
To get the content into a file as I described above I had to use stdbuf though I'm not sure why it is required (tr alone didn't work, the file will be empty without stdbuf):
timeout 5s dd if=/dev/random | pv -fr > /dev/null 2> >(stdbuf -oL tr '\r' '\n' > rates.log)
From man pv
:
- -f, --force
- Force output. Normally, pv will not output any visual display if standard error is not a terminal. This option forces it to do so.
Since rates.log
isn't a terminal, you need to do pv -fr
instead of pv -r
.
这篇关于将 pv 输出 (stderr) 打印到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!