Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I tested the following command, but it doesn't work.

$> top -b -d 1 | grep java > top.log

It doesn't use standard error. I checked that it uses standard output, but top.log is always empty. Why is this?

like image 283
JaycePark Avatar asked Aug 30 '13 05:08

JaycePark


Video Answer


1 Answers

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
like image 79
devnull Avatar answered Oct 01 '22 19:10

devnull