Home » Linux » Wait until tomcat finishes starting up

Wait until tomcat finishes starting up

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have a script that needs to run after tomcat has finished starting up and is ready to start deploying applications. I’m using $TOMCAT_HOME/bin/startup.sh which returns immediately. How can I wait until tomcat has finished starting up?

Answers:

There are probably several ways to do this. The trick we use is:

#!/bin/bash

until [ "`curl --silent --show-error --connect-timeout 1 -I http://localhost:8080 | grep 'Coyote'`" != "" ];
do
  echo --- sleeping for 10 seconds
  sleep 10
done

echo Tomcat is ready!

Hope this helps!

Questions:
Answers:

It’s not hard to implement programaticaly. You can implement org.apache.catalina.LifecycleListener and then you’ll have

public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
            if(lifecycleEvent.getType().equals(Lifecycle.START_EVENT))
            //do what you want
            }       
}

in web.xml :

<Context path="/examples" ...>
...
<Listener className="com.mycompany.mypackage.MyListener" ... >
...
</Context>

Please notice that some things could differ between 6-9 Tomcats.

Questions:
Answers:

Depends on what you mean by finishing. What do you want to wait for?

You could, for example, have a script that hits a URL repeatedly until it gets a desirable result that would only be available once the app is properly initialized.

You could also have a context listener that writes out an “I’m ready” file that you use to signal the readiness of your application. (If you do this, be sure the file doesn’t exist before starting your app container).

Questions:
Answers:

Are you still looking for an answer? It depends on your definition of started. If your definition of started is “Now its safe to stop” then you might want to verify if port 8005 is listening.

Questions:
Answers:

There isn’t an easy method. As far as startup.sh and catalina.sh are concerned, tomcat is running when they finish. Although, internally, tomcat is still initializing and starting contexts.

It would help to know if you were trying to find out if your context finished loading or if you are just wanting a general, “Tomcat is runnnig although your contexts might not be completely loaded…”

If it is the latter you could create a web app that simply has a context listener that will execute a script using Runtime. If you were handy, you could make the webapp configuable via the web.xml file to accept a parameter that points to the script to execute.

Questions:
Answers:

Personally, I would just watch catalinas log for a specific string depending on how your setup and what exact phase your looking for.

Questions:
Answers:

I needed this to test from jenkins if the tomcat from the remote server started for a system check.


until [[ `ssh -o StrictHostKeyChecking=no [email protected]${DEPLOY_HOST} 'netstat -tulpn | grep 8005'` != "" ]] ; do echo "waiting for tomcat"; sleep 6; done