After grep
exits, your tee
will exit due to SIGPIPE only when it tries to write more to the pipe. This is how pipes work. This means you need one more line from tail
. tee
will exit eventually, unless tail
is forever silent after printing SPEFICIC LOG MESSAGE
.
tail … | grep …
is similar in general. The trick with (tail … &)
makes your main shell not wait for tail
(it waits for the subshell instead, and the subshell does not wait for tail
thanks to &
).
You can use the same trick with tee
:
(tail -f -n 0 test.log &) | (tee /dev/fd/2 &) | grep -q 'SPEFICIC LOG MESSAGE'
The main shell will wait for the two subshells and grep
. The subshells will exit almost immediately, so it's grep
what matters. When grep
exits, the main shell will continue.
tee
will stay in the background until it tries to write more. Only then it gets SIGPIPE and exits. This requires one extra line from the log.
In turn tail
will stay in the background until it tries to write more after tee
exits. This requires another extra line from the log. Unless your tail
is smart. GNU tail
detects broken pipe immediately, it does not require the trick. Using the trick with GNU tail
is harmless, so when in doubt, use it.
tee
is not that smart, it does require the trick.
The command I gave you should work, but keep in mind tee
and tail
will stay in the background until the next line appears in the log. If your tail is not smart then it will stay for yet another line.