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

Ondra Žižka picture Ondra Žižka · Jun 23, 2011 · Viewed 15.4k times · Source

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.

Update: Seems like I should have emphasize that I want the process to end when the string is matched.

Answer

Ondra Žižka picture Ondra Žižka · Jun 23, 2011

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)