管理Tomcat服务的Daemon脚本

来源:互联网 发布:能看杂志的软件 编辑:程序博客网 时间:2024/06/06 10:05

一、脚本功能

# tomcat 的启动,重启,实时查看日志

#!/bin/bash  

# funtions: 
# chkconfig: 345 80 15  
# description: Multiple tomcats service management script.  
# author: shaonbean
# Source function library.  
. /etc/rc.d/init.d/functions  


# tomcat name
TOMCAT_PORT=$1
TOMCAT=tomcat_$1
TOMCAT_HOME=/opt/tomcats/${TOMCAT}
TOMCAT_BIN=${TOMCAT_HOME}/bin
#TOMCAT_LOG=${TOMCAT_HOME/logs/catalina.$(date +%Y-%m-%d).out
TOMCAT_LOG=${TOMCAT_HOME}/logs/catalina.out


#
# source function library.  
. /etc/rc.d/init.d/functions


if [ -f ${TOMCAT_BIN}/catalina.sh ];then
       echo "${TOMCAT} is valilable !!!"  
     else
       echo "${TOMCAT} not valilable !!!"
       exit
fi


RETVAL=$?  


start(){
        checkrun
        if [ $RETVAL -eq 0 ]; then  
                echo "-- Starting ${TOMCAT}... --"  
                bash ${TOMCAT_BIN}/startup.sh  
                touch /var/lock/subsys/${TOMCAT_PORT}
                checklog 
                status
        else  
                echo "-- ${TOMCAT} already running !"  
        fi  
}  


# 停止某一台tomcat,如果是重启则带re参数,表示不查看日志,等待启动时再提示查看  
stop(){
        checkrun  
        if [ $RETVAL -eq 1 ]; then  
                echo "-- Shutting down tomcat..."  
                ${TOMCAT_BIN}/shutdown.sh  
                if [ "$1" != "re" ]; then
        checklog
                else
                  sleep 5
                fi
                rm -rf /var/lock/subsys/${TOMCAT_PORT} 
                status
           else  
                echo "-- ${TOMCAT} not running"  
        fi  
      }  


status(){
        checkrun
        if [ $RETVAL -eq 1 ]; then
                echo -n "-- ${TOMCAT} ( pid "  
                ps ax --width=1000 | grep ${TOMCAT_PORT}| grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}'
                              echo -n ") is running..."  
                echo  
          else
                echo "-- ${TOMCAT} is stopped"  
        fi
        #echo "---------------------------------------------"  
}


# 查看tomcat日志,带vl参数
log(){
        status
        checklog yes
}


# 如果tomcat正在运行,强行杀死tomcat进程,关闭tomcat
kill(){
        checkrun
        if [ $RETVAL -eq 1 ]; then
            read -p "-- Do you really want to kill ${TOMCAT_PORT} progress?[no])" answer
            case $answer in
                Y|y|YES|yes|Yes)
                    ps ax --width=1000 | grep ${TOMCAT_PORT} | grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}'| xargs kill -9  
                    status
                ;;
                *);;
            esac
        else
            echo "-- exist with ${TOMCAT_PORT} still running..."
        fi
     }   


checkrun(){  
           ps ax --width=1000 |grep ${TOMCAT_PORT}| grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 "\n"}' | wc -l > /var/run/tomcat_process_count
           read line < /var/run/tomcat_process_count 
           if [ $line -gt 0 ]; then  
                  RETVAL=1  
                  return $RETVAL  
             else  
                  RETVAL=0  
                  return $RETVAL  
           fi  
}  
# 如果是直接查看日志viewlog,则不提示输入[yes],否则就是被stop和start调用,需提示是否查看日志
checklog(){
           answer=$1
           if [ "$answer" != "yes" ]; then
              read -p "-- See Catalina.out log to check $2 status?[yes])" answer
           fi
           case $answer in
                           Y|y|YES|yes|Yes|"")
                           tail -f ${TOMCAT_LOG}
                                            ;;
                                            *)
                                   #    status
                                   #    exit 0
                                            ;;
           esac
          }


checkexist(){
             if [ ! -d ${TOMCAT_HOME} ]; then
                 echo "-- ${TOMCAT_HOME} does not exist."
                 exit 0
             fi
            }      
case "$2" in  
            start)  
                 checkexist
                 start  
                 exit 0
                 ;;  
             stop)  
                 checkexist
                 stop  
                 exit 0
                 ;;  
          restart)  
                 checkexist
                 stop re 
                 start 
                 exit 0
                 ;;  
           status)  
                 checkexist
                 status  
        #${TOMCAT_BIN}/catalina.sh version  
                 exit 0
                 ;;  
             log)
                checkexist
                log
                exit 0
                ;;
            kill)
                checkexist
                status
                kill
                exit 0
                ;;
               *)  
        echo "Usage: $0 {start|stop|restart|status|log|kill}"  
        echo "service tomcat {8080|8081|..} {start|stop|restart|status|log|kill}"  
esac  
exit 0
0 0
原创粉丝点击