Linux: Block until a string is matched in a file (“tail + grep with blocking”)

后端 未结 7 1686
小蘑菇
小蘑菇 2020-12-05 11:58

Is there some one-line way in bash/GNU tools to block until there\'s a string matched in a file? Ideally, with timeout. I want to avoid multi-line loop.

Upda

7条回答
  •  攒了一身酷
    2020-12-05 12:34

    Thanks both for answers, but the important part was that the process blocks until found, then ends. I found this:

    grep -q 'PATTERN' <(tail -f file.log)
    

    -q is not much portable, but I will only use Red Hat Enterprise Linux so it's ok. And with timeout:

    timeout 180 grep -q 'PATTERN' <(tail -f file.log)
    

提交回复
热议问题