Saturday, January 21, 2023

Piping output of top through grep does not give any output

Recently I was trying to grep for "load average" in the output of top and then redirect it to a file to save for later analysis - however the output file would be totally empty for quite some time. That is the time I realized that grep was buffering its output causing the output file to be empty. So the grep command was not writing to the file immediately but was collecting large amounts of data in its buffer before flushing that out to the file.

My original command was

top -b -u myusername -d 5 | grep -i 'load average' > /tmp/load_averages.log

Because this did not write anything to the output file for quite a while, I had to change it buffer only till it saw a newline in the output and then to flush out whatever it has gathered so far in its buffer as below:

top -b -u myusername -d 5 | grep -i 'load average' --line-buffered > /tmp/load_averages.log

And lo behold, tailing the output file shows the load averages being printed every 5 seconds (because of the -d 5 flag given to the top command)

No comments:

LinkWithin

Related Posts Plugin for WordPress, Blogger...