How to automatically recover Tomcat from crashes

Saturday, September 17, 2016

How to automatically start Tomcat if tomcat automatically down :-

There are 2 Process :-

1. if your tomcat running via /etc/init.d/tomcat than we can use below shell script

#! /bin/sh 
SERVICE=/etc/init.d/tomcat 
STOPPED_MESSAGE="Tomcat Servlet Container is not running." 
 
if [ "`$SERVICE status`" == "$STOPPED_MESSAGE"]; 
then 

  $SERVICE start 

fi 



2. if I don’t have /etc/init.d/tomcat* script than use below shell script


#! /bin/sh   
CATALINA_PID=/var/run/tomcat.pid; export CATALINA_PID

TOMCAT_HOME=/usr/local/tomcat   
if [ -f $TOMCAT_HOME/bin/tomcat.pid ] 
then 
  echo "PID file exists" 
  pid="`cat $TOMCAT_HOME/bin/tomcat.pid`" 
  if [ "X`ps -p $pid | awk '{print $1}' | tail -1`" != "XPID"] 
  then 
    echo "Tomcat is running" 
  else 
    echo "Tomcat had crashed" 
    $TOMCAT_HOME/bin/startup.sh 
  fi 
else 
  echo "PID file does not exist. Restarting..." 
  $TOMCAT_HOME/bin/startup.sh 
fi



Set your cron job via
crontab -e 

# monitor tomcat every 10 minutes 
*/10 * * * * /root/recover-tomcat.sh

or

# monitor tomcat every 2 minutes  
*/2 * * * * /root/recover-tomcat.sh  

 

0 comments:

About This Blog

Lorem Ipsum

  © Copyright 2009 Linux-HelpLine.Blogspot.com

Back to TOP