Dave Dave - 7 months ago 9
Bash Question

In my bash script, how do I write a while loop that only exits if the output of "tail" doesn't contain a string?

I’m using Amazon Linux with bash shell. In my bash script, how do I construct a while loop that will spin so long as the command

tail -10 /usr/java/jboss/standalone/log/server.log


does not contain the string “FrameworkServlet ‘myprojectDispatcher': initialization completed”?

Answer

You can use:

tail -n 10 -f /usr/java/jboss/standalone/log/server.log |
awk '/FrameworkServlet.*myprojectDispatcher.*initialization completed/{exit} 1'

awk will exit when it encounters search string otherwise it will keep writing input to stdout.

However do keep in mind that the tail command is buffered and to avoid that behavior try stdbuf gnu utility:

stdbuf -i0 -o0 -e0 tail -n 10 -f /usr/java/jboss/standalone/log/server.log |
awk '/FrameworkServlet.*myprojectDispatcher.*initialization completed/{exit} 1'
Comments