Tail Command – Using ‘tail -f’ Until Text is Seen

logstail

I've got a CI server with a command-line interface that allows me to remotely kick-off a job (jenkins CI server and the jenkins-cli.jar tool).

After I kick the job off I tail -f the log (sorry for the messy command):

ssh -t my-jenkins-host.com "tail -f \"/var/lib/jenkins/jobs/$job_name/builds/\`ls -ltr /var/lib/jenkins/jobs/$job_name/builds/ | grep '^l' | tail -n 1|awk '{print \$9}'\`/log\""

After the job successfully completes, usually after at least 5 minutes, I get the following line on the output:

Finished: SUCCESS

Is there a good way to stop tailing the log at this point? i.e. is there like a tail_until 'some line' my-file.log command?

BONUS: extra credit if you can supply an answer that returns 0 when SUCCESS is matched, 1 when FAILURE is matched, and your solution works on mac! (which i believe is bsd based)

Best Answer

You can pipe the tail -f into sed, telling it to quit when it sees the line you're searching for:

tail -f /path/to/file.log | sed '/^Finished: SUCCESS$/ q'

sed will output each line it processes by default, and exit after it sees that line. The tail process will stop when it tries to write the next line and sees its output pipe is broken