管理tomcat单机多实例脚本

2017-06-12  本文已影响6人  loveroot
#!/bin/bash

# Apache Tomcat daemon
# chkconfig: 345 10 10
# description: Apache Tomcat daemon
# processname: tomcat
                                 

#定义JAVA_HOME
export JAVA_HOME=/app/jdk1.8

#定义多个tomcat的总目录,/app/tomcatserver目录下有tomcat6001,tomcat7001,tomcat8001,tomcat9001 四个tomcat实例。
tom="/app/tomcatserver/tomcat"

#定义启动脚本路径
startup_bin="bin/startup.sh"

#定义tomcat的启动方式,启动方式为tomcat.sh p1 start类似的命令
usage="{p1 args |p2|p3|all} {start|stop|restart|status}"

dev="/dev/null"

#定义如何启动tomcat,在此我们是通过个数tomcat以及前面定义的tomcat的命令,来操作tomcat

#judge $1 $2 whether null

if [ "$1" == "" -o "$2" == "" ];then
    echo "Usage: $0 $usage"
    exit 1
fi

#judge $1

case $1 in
   "p1")
    tomcats=$2
;;
   "p2")
    tomcats="6001 7001"
;;
   "p3")
    tomcats="6001 7001 8001"
;;
   "all")
    tomcats="6001 7001 8001 9001"
;;
#   *)
#   echo "Usage: $0 $usage"
#;;
esac

#定义tomcat的启动
#define start function

tomcatstart() {
    for i in $tomcats
        do
            tom_home="$tom$i"
            run_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom_home}")

            if [ "${run_status}X" != "X" ];then
                echo "tomcat$i is already running…"
            else
                ${tom_home}/${startup_bin} &>$dev
                echo "tomcat$i starting,Please wait 2s…"
                sleep 2
            fi
        done
    }

#定义tomcat的关闭
#define stop function

    tomcatstop() {
      for j in $tomcats
        do
            tom1_home="$tom$j"
            tomcat_pid=$(ps -ef | grep ${tom1_home} | grep "org.apache.catalina.startup.Bootstrap start" | awk '{printf $2}')
            if [ "${tomcat_pid}X" == "X" ];then
                echo "tomcat$j is not running…"
            else
                kill -9 ${tomcat_pid} & >$dev
                echo "tomcat$j stopping,Please wait 1s…"
                sleep 1
                echo "delte tomcat$j cache,Please wait 1s…"
                rm -rf ${tom1_home}/work/*
            fi
        done
    }

#定义tomcat的重启
#define restart function

tomcatrestart() {

    for m in $tomcats
        do
            tom2_home="$tom$m"
            run2_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom2_home}")
            if [ "${run2_status}X" == "X" ];then
                echo "tomcat$m is not running…"
                ${tom2_home}/${startup_bin} &>$dev
                echo "tomcat$m starting,Please wait 2s…"
                sleep 2
            else
                ps -ef | grep ${tom2_home} | grep "org.apache.catalina.startup.Bootstrap start" | awk '{printf $2}'| xargs kill -9 >$dev
                echo "tomcat$m stopping,Please wait 2s…"
                sleep 1
                ${tom2_home}/${startup_bin} &>$dev
                echo "tomcat$m starting,Please wait 2s…"
                sleep 2
            fi
        done
    }

#定义tomcat的状态

#define status function

    tomcatstatus() {
        for n in $tomcats
            do
                tom3_home="$tom$n"
                run3_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom3_home}")
            if [ "${run3_status}X" == "X" ];then
                echo "tomcat$n is not running…"
                else
                echo "tomcat$n is running"
            fi
        done
    }


    #judge $2 $3
    if [ "$3" == "" ];then
        case $2 in
        "start")
        tomcatstart
        ;;
        "stop")
        tomcatstop
        ;;
        "restart")
        tomcatrestart
        ;;
        "status")
        tomcatstatus
        ;;
        *)
        echo "Usage: $0 $usage"
        ;;
        esac
    else
        case $3 in
                "start")
                tomcatstart
            ;;
                "stop")
                tomcatstop
            ;;
                "restart")
                tomcatrestart
            ;;
                "status")
                tomcatstatus
            ;;
                *)
                echo "Usage: $0 $usage"
            ;;
            esac
    fi
上一篇下一篇

猜你喜欢

热点阅读