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

相关文章
相关标签/搜索