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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With