Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait until tomcat finishes starting up

Tags:

java

linux

tomcat

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?

like image 206
timdisney Avatar asked Dec 17 '08 22:12

timdisney


People also ask

Why does Tomcat take so long to start?

If you have installed Tomcat 8 on your Linux server and it is taking forever to (re)start, it is not your fault. It happens when Java Runtime Environment (JRE) entropy source is short of entropy. Tomcat 8+ relies on SecureRandom class to provide randomly generated values for its session ids and in other places.

What happens when Tomcat starts?

the file gets compiled into target/classes. upon publish the file gets copied to the deployment folder. Tomcat notices that a class file was changed and reloads the context (i.e. web application is restarted)

How do I know if Tomcat is started?

Use a browser to check whether Tomcat is running on URL http://localhost:8080 , where 8080 is the Tomcat port specified in conf/server. xml. If Tomcat is running properly and you specified the correct port, the browser displays the Tomcat homepage.


3 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!

like image 159
Matt Solnit Avatar answered Oct 06 '22 06:10

Matt Solnit


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.

like image 8
daoway Avatar answered Oct 06 '22 05:10

daoway


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.

like image 2
The Governor Avatar answered Oct 06 '22 07:10

The Governor