Print pv output (stderr) to file

和自甴很熟 提交于 2021-02-10 05:33:09

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/61864319/print-pv-output-stderr-to-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!