Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

tomcat

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?

like image 619
gotch4 Avatar asked Feb 26 '23 05:02

gotch4


2 Answers

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.

like image 135
mindas Avatar answered Feb 27 '23 17:02

mindas


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
like image 23
Zé Carlos Avatar answered Feb 27 '23 18:02

Zé Carlos