Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 774
Ondra Žižka Avatar asked Jun 23 '11 13:06

Ondra Žižka


1 Answers

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)
like image 136
Ondra Žižka Avatar answered Oct 14 '22 13:10

Ondra Žižka