gotch4
gotch4

Reputation: 13269

How to restart Tomcat on a remote server if the application crashes?

Suppose I put a java app on Tomcat on a remote server, say Amazon AWS. What's do you recommend to restart tomcat AUTOMATICALLY if the application fails in a unrecoverable manner? Maybe is there a way to do this from the app itself, so that if I see that the exception is very nasty I can restart it all?

Upvotes: 4

Views: 16656

Answers (3)

Zé Carlos
Zé Carlos

Reputation: 3807

Interesting solution without any programs here: http://aujava.wordpress.com/2006/08/16/watchdog-for-tomcat/

You just need to add isalive.html (with the single text "YES") to your application and use the following script:

#!/bin/sh
HOST=127.0.0.1
PORT=8080

#infinite loop
while [ 1 ]
do
    #try to access tomcat's page
    RES=`wget -O - -o /dev/null --proxy=off http://${HOST}:${PORT}/isalive.html | awk '{ print $1 }'`
    echo got ${RES}
    #decide on reply
if [ "$RES" = "YES" ]
then
    echo tomcat is responding on $HOST:$PORT
else
    echo tomcat seems to be dead.
    echo Killing...
    for thepin in `ps -Af | grep -v grep | grep tomcat | grep catalina | awk '{ print $2 }'`
    do
        kill -9 ${thepin}
    done
    echo Starting...
    sudo -u tomcat /usr/local/tomcat/bin/startup.sh
fi

sleep 60
done

Upvotes: 3

Ruslan
Ruslan

Reputation: 1024

I would recommend to look at monit utility. With monit you can easily monitor service, resource usage, check urls - to make sure that service responding as expected, and initiate restart when something wrong http://mmonit.com/monit/documentation/monit.html#connection_testing_using_the_url_notation

Upvotes: 1

mindas
mindas

Reputation: 26713

One possibility would be to install a watchdog which monitors (e.g. on a port, some custom check, etc.) the app and restarts entire server if necessary. This can even be a bash script which does catalina.sh run on a controlled sub-shell.

Decent monitoring systems also allow this. For example, Zabbix allows custom monitoring checks and actions so if a service is unreachable it can proactively restart it.

Another solution would be to use Tomcat manager to stop/start existing application. You can do this via Apache Ant script that invokes relevant manager URL. This solution is however not applicable if the application dies "hard" and takes the entire server down.

Upvotes: 4

Related Questions