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"
Share:
53,116

Related videos on Youtube

Sten Kin
Author by

Sten Kin

Updated on May 01, 2020

Comments

  • Sten Kin
    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
      Keith Thompson about 10 years
      I often do exactly what you're describing, and it works. The grep command doesn't terminate until the tail -f process terminates, but of course it doesn't produce any output until something containing the pattern is appended to the file.
    • Rob Napier
      Rob Napier about 10 years
      I 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
      anubhava about 10 years
      Issue is it only run grep once and is done no that's not right.
    • Chris Stratton
      Chris Stratton about 10 years
      I 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
      Sten Kin about 10 years
      Ah chaining was the issue! That fixed it @ChrisStratton