【问题标题】:Tail -f + grep? [duplicate]尾 -f + grep? [复制]
【发布时间】:2014-06-17 05:18:19
【问题描述】:

Tail 有以下选项:

-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.

我只想在尾部输出中为something grep。

tail -f <FILE> | grep <SOMETHING> 

问题是它只运行一次 grep 就完成了。没有其他输出发生。如何使用-f 使 grep 正确运行?

【问题讨论】:

  • 我经常按照您的描述进行操作,而且效果很好。 grep 命令在 tail -f 进程终止之前不会终止,但当然它不会产生任何输出,直到将包含该模式的内容附加到文件中。
  • 我可以在多个平台上确认这一点。完全按照这里的描述使用tail+grep,我从来没有遇到过麻烦。您在哪个平台上遇到问题?
  • Issue is it only run grep once and is done 不,这是不对的。
  • 我不认为你会在这里遇到它,但是在使用 grep 的较长管道链中要小心(特别是多次使用),它可能会因块缓冲而失效,因此不会产生任何输出相当长一段时间,除非你明确指定 --line-buffered
  • 啊链接是问题!这解决了它@ChrisStratton

标签: linux bash unix grep tail


【解决方案1】:

您会发现另一个 SO 问题很有帮助:How to 'grep' a continuous stream?

开启 grep 的行缓冲模式。

tail -f file | grep --line-buffered my_pattern

【讨论】:

    【解决方案2】:

    如果这是一个日志文件,它可能会被轮换。然后它将停止提供数据。
    如果文件被旋转,这不会停止。

    tail --follow=name /var/log/syslog | grep "some data"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多