Friday 20 May 2016

Managing tomcat application

Tomcat is a servlet application which is ready to use after download. There is no need of installing this application. We can directly start or stop it from the bin directory. 

So in such case we should go to the bin directory in the CATALINA_HOME or else we need to use the startup script along with its absolute path. And the same is needed to shut it down.

We can use the below script to manage the tomcat application and we neither need to go to the CATALINA_HOME nor to use the absolute path. All we need to do is to change the permissions to make it executable and copy that to /sbin and /etc/init.d/ directories. If we copy this into /etc/init.d/ then also we need to mention the absolute path but we can easily memorize it as we know all the startup scripts exists there. But if we copy that into /sbin then there is absolutely no need of mentioning the path of the file.

We can use this like below:
 # tomcat start|stop|restart|status  
#!/bin/bash  
# Author : Arjun Shrinivas  
# Purpose : This script is usefull for managing tomcat  
   
 CATALINA_HOME=/u01/tomcat  
 OPTION=$1  
   
 echo "Usage: `basename $0` start|stop|restart|status"  
   
 case $OPTION in  
 start)  
      echo "Starting tomcat"  
      $CATALINA_HOME/bin/startup.sh  
 ;;  
 stop)  
      echo "Stopping tomcat"  
      $CATALINA_HOME/bin/shutdown.sh  
 ;;  
 restart)  
      PID=`ps -ef | grep catalina | grep -v grep | awk '{print $2}'`  
      if [ -z $PID ]  
      then  
           echo "Starting tomcat"  
           $CATALINA_HOME/bin/startup.sh  
      else  
           echo "Stopping tomcat"  
           $CATALINA_HOME/bin/shutdown.sh  
           sleep 20  
           PID=`ps -ef | grep catalina | grep -v grep | awk '{print $2}'`  
           if [ -z $PID ]  
           then  
                echo "Starting tomcat"  
                $CATALINA_HOME/bin/startup.sh  
           else  
                echo -e "Unable to shutdown tomcat. Force killing tomcat PID : $PID"  
                kill -9 $PID  
                echo "Starting tomcat"  
                $CATALINA_HOME/bin/startup.sh  
           fi  
      fi  
 ;;  
 status)  
      PID=`ps -ef | grep catalina | grep -v grep | awk '{print $2}'`  
      if [ ! -z $PID ]  
      then  
           echo -e "Tomcat is running on pid : $PID"  
      else  
           echo -e "Tomcat is not running"  
      fi  
 ;;  
 *)  
      echo "Invalid operation"  
 ;;  
 esac  
   
 # END