BASH: Wait for log entry

For a startup script, I needed to start JBoss and start another component as soon as the complete JBoss server was started. When you execute the “run.sh” script that comes with JBoss, it immediately exits and starts JBoss in the background (which is quite nice I think). Unfortunately, when I started the other component using this method, the additional program was unhappy, since JBoss was not ready yet. So I had to come up with a trick to delay the start of the additional program.

So with the help of the internet and my co-worker, we came up with the following script:

#!/bin/bash
STARTJBOSS="/opt/jboss/bin/jboss_init_redhat.sh"
LOGFILE="/opt/jboss/server/minimal/log/server.log"
$STARTJBOSS start
while read LINE; do
    if [[ $LINE =~ JBoss\ \\\(MX\ MicroKernel\\\).*Started\ in ]]; then
        echo "JBoss started!"
        break
    fi
done < <(tail -f $LOGFILE)
# Do something here

Basically, you start the JBoss server by invoking the script that comes with the JBoss package. Then, we use tail -f with a while-loop to look at every line. Using a regular expression, we wait for the “JBoss [..] Started in [..]” log entry and use break to get out of the while-loop. The script then continues like normal.

Hello world

My name is Simon Krenger, I am a Technical Account Manager (TAM) at Red Hat. I advise our customers in using Kubernetes, Containers, Linux and Open Source.

Elsewhere

  1. GitHub
  2. LinkedIn
  3. GitLab