@anubhava's answer works, but there is no need for awk
:
The key is to use grep
with -m1
, which makes it return once a match is found, at which point the tail
process can be killed:
tail -f server.log |
{ grep -m1 "Started successfully with " && pkill -P $$ -x tail; }
-P $$
(considers only child processes of the current shell) and-x
(matches process name exactly) ensure that only thetail
process of interest is killed.- You could also use a subshell
(...)
instead of command grouping{ ...; }
(slightly less efficient). - If the string is not found,
grep
only returns in the event thattail
is externally killed - to report this case, add a||
clause; e.g.,|| echo 'ERROR: tail -f was killed' >&2