Why does 'top | grep > file' not work? Why does 'top | grep > file' not work? shell shell

Why does 'top | grep > file' not work?


By default, grep buffers output which implies that nothing would be written to top.log until the grep output exceeds the size of the buffer (which might vary across systems).

Tell grep to use line buffering on output. Try:

top -b -d 1 | grep --line-buffered java > top.log


In my embedded machine, grep hadn't the --line-buffered option. So I used this workaround for my myself:

while :;do top -b -n 1 | grep java >> top.log;done &

By this way I could have a running monitor in the background for a program like "java" and keep all results in the file top.log.