26

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?

Sten Kin
  • 2,485
  • 3
  • 23
  • 29
  • 5
    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. – Keith Thompson Apr 30 '14 at 18:42
  • 2
    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? – Rob Napier Apr 30 '14 at 18:42
  • 1
    `Issue is it only run grep once and is done` no that's not right. – anubhava Apr 30 '14 at 18:43
  • 1
    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 – Chris Stratton Apr 30 '14 at 18:43
  • Ah chaining was the issue! That fixed it @ChrisStratton – Sten Kin Apr 30 '14 at 18:45

2 Answers2

68

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
Community
  • 1
  • 1
Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
10

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"
Jotne
  • 40,548
  • 12
  • 51
  • 55