Article 5PM8S tail+grep woes

tail+grep woes

by
rihad
from LinuxQuestions.org on (#5PM8S)
I have the following code:

timeout 10 sh -c 'tail -q -n50 -F /some/file.log | grep -qE foo'

Which is supposed to work as follows: tail fetches the last 50 files from a log file and continues fetching newly written data until a match is found by grep and it silently exits with success (as instructed by the -q flag). This works if tail writes following lines after grep exits, receives SIGPIPE or similar, and the whole pipeline exits with grep's exit status, which is 0. Nice and candy. The problem is that when both of the following are true: a match is found resulting in grep's successful completion AND no more data is written to the log file, tail doesn't know that the reader of the pipeline has exited and continues "watching" the file for new data (as commanded by -F flag) until the timeout of 10 seconds passes and timeout exits with 143 (128+SIGTERM, which is 15).

Is there any way to make the whole pipeline exit immediately with grep's exit code no matter what?latest?d=yIl2AUoC8zA latest?i=fLcYyxuAUQk:jd17Yek4pLo:F7zBnMy latest?i=fLcYyxuAUQk:jd17Yek4pLo:V_sGLiP latest?d=qj6IDK7rITs latest?i=fLcYyxuAUQk:jd17Yek4pLo:gIN9vFwfLcYyxuAUQk
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments