Tail -f + grep?
53,116
Solution 1
You will find another SO Question helpful: How to 'grep' a continuous stream?
Turn on grep's line buffering mode.
tail -f file | grep --line-buffered my_pattern
Solution 2
If this is a logfile it may be rotated. It will then stop giving data.
This will not stop if file is rotated.
tail --follow=name /var/log/syslog | grep "some data"
Related videos on Youtube
Author by
Sten Kin
Updated on May 01, 2020Comments
-
Sten Kin about 4 years
Tail has the following options:
-f The -f option causes tail to not stop when end of file is reached, but rather to wait for additional data to be appended to the input. The -f option is ignored if the standard input is a pipe, but not if it is a FIFO.
I'd like to only grep for
something
in the tail output.tail -f <FILE> | grep <SOMETHING>
Issue is it only run grep once and is done. No other output happens. How can I make grep run correctly with the
-f
?-
Keith Thompson about 10 yearsI often do exactly what you're describing, and it works. The
grep
command doesn't terminate until thetail -f
process terminates, but of course it doesn't produce any output until something containing the pattern is appended to the file. -
Rob Napier about 10 yearsI can confirm the same on several platforms. I've never had trouble using tail+grep exactly as described here. What platform are you having trouble on?
-
anubhava about 10 years
Issue is it only run grep once and is done
no that's not right. -
Chris Stratton about 10 yearsI don't think you'll hit it here, but be careful in longer piped chains with grep (especially used more than once) that it may defult to a block buffering and thus not produce any output for quite some time, unless you explicitly specify --line-buffered
-
Sten Kin about 10 yearsAh chaining was the issue! That fixed it @ChrisStratton
-