Sunday, 5 June 2016

How to check the Apache Tomcat Status with bash script?


##check the status of tomcat
is_tomcat_running(){
        PID=$(cat $TOMCAT/conf/catalina.pid 2>&1)
        if [ "x$PID" != "x" ]&& kill -0 $PID 2>/dev/null; then
                _status=0 #echo "tomcat already running"
        else
                _status=1 #echo "tomcat is not running"
        fi
        return $_status
}

How to stop the Tomcat with bash script?


stop() {
  pid==$(cat $TOMCAT/conf/catalina.pid 2>&1)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    #/bin/su -p -s /bin/sh $TOMCAT_USER
        sh $CATALINA_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      terminate
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}


Note: We need to kill/terminate the tomcat if it is failed to shutdown with in the given period of time& we need to clear the PID.

How to check Apache Web Server Status in Ubuntu with bash script?


##Check Apache Web Server running
is_apache_running(){
        a=$(service apache2 status) >> /dev/null
        if [ "$a" = " * apache2 is running" ];
        then
                _status=0  #echo "Apache WebServer Already Running"
        else
                _status=1  #echo "Apache WebServer is not Running"
        fi
        return $_status
}


##Check Apache Syntax
is_apache_syntax_ok(){

_syntax=$(apache2ctl -t 2>&1)

if [ "$_syntax" = "Syntax OK" ]; then
         _syntax_ok=0
else
         _syntax_ok=1
fi
        return  $_syntax_ok
}

Above snippet is tested under Ubuntu Server 12.x & 14.x.