问题
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