centos7 同時管理多個tomcat

#!/bin/bash  
# author: Sean Chow (seanlook7@gmail.com)

#  
# chkconfig: 345 80 15  
# description: Multiple tomcats service management script.  apache

# Source function library.  
. /etc/rc.d/init.d/functions  tomcat

# 第幾個tomcat
tcNo=$1
tcName=tomcat$1
basedir=/apps/test/$tcName
tclog=${basedir}/logs/catalina.$(date +%Y-%m-%d).outbash

RETVAL=0  app

start(){
        checkrun  
        if [ $RETVAL -eq 0 ]; then  
                echo "-- Starting tomcat..."  
                $basedir/bin/startup.sh  
                touch /var/lock/subsys/${tcNo}
                checklog 
                status
        else  
                echo "-- tomcat already running"  
        fi  
}  rest

# 中止某一臺tomcat,若是是重啓則帶re參數,表示不查看日誌,等待啓動時再提示查看  
stop(){
        checkrun  
        if [ $RETVAL -eq 1 ]; then  
                echo "-- Shutting down tomcat..."  
                $basedir/bin/shutdown.sh  
                if [ "$1" != "re" ]; then
                  checklog
                else
                  sleep 5
                fi
                rm -f /var/lock/subsys/${tcNo} 
                status
        else  
                echo "-- tomcat not running"  
        fi  
}  日誌

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

# 查看tomcat日誌,帶vl參數
log(){
        status
        checklog yes
}進程

# 若是tomcat正在運行,強行殺死tomcat進程,關閉tomcat
kill(){
        checkrun
        if [ $RETVAL -eq 1 ]; then
            read -p "-- Do you really want to kill ${tcName} progress?[no])" answer
            case $answer in
                Y|y|YES|yes|Yes)
                    ps ax --width=1000 |grep ${tcName}|grep "org.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}'|xargs kill -9  
                    status
                ;;
                *);;
            esac
        else
            echo "-- exit with $tcName still running..."
        fi
}ip


checkrun(){  
        ps ax --width=1000 |grep ${tcName}| grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}' | wc | awk '{print $2}' >/tmp/tomcat_process_count.txt  
        read line < /tmp/tomcat_process_count.txt  
        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 ${tclog}
            ;;
            *)
            #    status
            #    exit 0
            ;;
        esac
}
checkexist(){
        if [ ! -d $basedir ]; then
            echo "-- tomcat $basedir does not exist."
            exit 0
        fi
}it


case "$2" in  
start)  
        checkexist
        start  
        exit 0
        ;;  
stop)  
        checkexist
        stop  
        exit 0
        ;;  
restart)  
        checkexist
        stop re 
        start 
        exit 0
        ;;  
status)  
        checkexist
        status  
        #$basedir/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 {0|1|..} {start|stop|restart|status|log|kill}"  
        esac  

exit 0

 

使用說明:
1. 使用前設定好baseDir(多tomcat所在路徑),各tomcat命名如tomcat0tomcat1...
2. 腳本名字爲tomcat,放到/etc/init.d/下,並基於可執行權限chmod +x /etc/init.d/tomcat
3. 執行用戶不容許用root,特別是在線上環境
4. 已處理其餘錯誤參數輸入,可用於正式環境
5. 你也能夠修改tcName來適應管理一個tomcat服務的情形
6. 使用,如下針對tomcat0/apps/test/tomcat0

相關文章
相關標籤/搜索