简体   繁体   English

使用bash shell脚本启动和停止多个tomcat

[英]start and Stop Multiple tomcat with bash shell script

I wrote this sh script to stop and start multiple tomcat with this command on ubuntu terminal 我写了这个sh脚本来在ubuntu终端上使用此命令停止和启动多个tomcat

sh apacheStopStartScript.sh start.

By starting this script the logs file are created there where my apacheStopStartScript.sh script is placed but I want the logs will created in there respective tomcat's bin/logs folder. 通过启动此脚本,将在其中放置我的apacheStopStartScript.sh脚本的位置创建日志文件,但是我希望将在相应的tomcat的bin / logs文件夹中创建日志。

Any One resolve this issue? 有人可以解决这个问题吗?

#!/bin/bash
# script Name: apacheStopStartScript.sh
# Script is run by command: sh apacheStopStartScript.sh start
### END INIT INFO
TOMCATNAME=""
TOMCATCOMMANPATH="/root/siteforge/"
TOMCATFIRST="apache-two-siteforge"
TOMCATSECOND="apache-three-siteforge"
TOMCATTHIRD="apache-four-siteforge"
TOMCATFOURTH="apache-five-siteforge"
TOMCATFIFTH="apache-six-siteforge"
NOW=""
LOG=""
check=""
count=0
# This Function Is Used To Find Running Tomcat Process Id
tomcat_pid() {
   echo `ps -efa | grep $TOMCATNAME | grep tomcat | grep java | egrep -v grep | awk '{print $2}'`
}

# This Function Is Used To Run/Start TOMCAT
start() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
        echo "TOMCAT : $TOMCATNAME is already running with (processId: $pid)"
    else     
        echo `nohup $TOMCATCOMMANPATH$TOMCATNAME/bin/catalina.sh run>>$LOG &`
        #   echo `nohup catalina.sh run>>logs/today.log &`      
        echo  "\n"
        check=$(cat $LOG | grep "Server startup in" | wc |tr -s ' ' | cut -d ' ' -f 4)
        while [ $check -eq $count ]
        do
            echo "Starting TOMCAT : $TOMCATNAME "
            check_count=$(cat $LOG | grep "Server startup in" | wc |tr -s ' ' | cut -d ' ' -f 4)
            check=`expr $check + $check_count `
            sleep 25
                if [ $check -gt $count ]
                then
                break
                fi
        done
        cat $LOG
        echo  "\n -------------------------TOMCAT :$TOMCATNAME STARTED SUCCESSFULLY----------------------------"
        echo "\n------------------------------------------------------------------------------------------------"
        echo `ps -efa | grep tomcat`
    fi
    return 0
}

# This Function Is Used To Stop TOMCAT 
stop() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
        echo "Killing TOMCAT : $TOMCATNAME processes with (processId: $pid)"
        kill -9 $(tomcat_pid)  
        echo "\n -------------------------STOPPED TOMCAT :$TOMCATNAME ---------------------------------------" 
    else
        echo "TOMCAT : $TOMCATNAME is not running"
    fi
return 0 
}

# This Function Is Used To terminate/kill running Tomcat by process Id
terminate() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
        echo "Killing processes with (processId: $pid)"
        kill -9 $(tomcat_pid)  
    else
        echo "No Such Process Is Running"
    fi

    return 0
}

# These are calling function name i.e start, stop, restart,kill,status 
case $1 in
    start)

        TOMCATNAME=$TOMCATFIRST
        NOW="$(date +'%Y-%m-%d-%T')"
        LOG="$TOMCATCOMMANPATH$TOMCATNAME/bin/logs/$NOW.log"
        stop
        sleep 10
        start

        read -p "Are you sure you want to start $TOMCATSECOND? <Y/N> " prompt1
        case $prompt1 in
        [Yy]*)
            TOMCATNAME=$TOMCATSECOND
            NOW="$(date +'%Y-%m-%d-%T')"
            LOG="$TOMCATCOMMANPATH$TOMCATNAME/bin/logs/$NOW.log"
            stop
            sleep 10
            start 
        ;;
        [Nn]*)
         echo "Successfully Exit"
         exit;;
         esac

            TOMCATNAME=$TOMCATTHIRD
            NOW="$(date +'%Y-%m-%d-%T')"
            LOG="$TOMCATCOMMANPATH$TOMCATNAME/bin/logs/$NOW.log"
            stop
            sleep 10
            start 

            TOMCATNAME=$TOMCATFOURTH
            NOW="$(date +'%Y-%m-%d-%T')"
            LOG="$TOMCATCOMMANPATH$TOMCATNAME/bin/logs/$NOW.log"
            stop
            sleep 10
            start 

            TOMCATNAME=$TOMCATFIFTH
            NOW="$(date +'%Y-%m-%d-%T')"
            LOG="$TOMCATCOMMANPATH$TOMCATNAME/bin/logs/$NOW.log"
            stop
            sleep 10
            start 
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
     kill)
        terminate
    ;; 
    status)
       pid=$(tomcat_pid)
        if [ -n "$pid" ]
        then
           echo "TOMCAT : $TOMCATNAME is running with processId: $pid"
        else
           echo "TOMCAT : $TOMCATNAME is not running"
        fi
        ;;
esac 
exit 0

Generally you can make your life easier by referring to catalina.sh to stop/start your tomcat for you: 通常,您可以通过参考catalina.sh来为您停止/启动tomcat,从而使您的生活更轻松:

so in your start process: 所以在您的启动过程中:

su - $TOMCAT_USER -c "$TOMCAT_PATH/catalina.sh start"

within catalina.sh you will see: 在catalina.sh中,您将看到:

if [ -z "$CATALINA_OUT" ] ; then
  CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
fi

here is an example: 这是一个例子:

https://gist.github.com/miglen/5590986 https://gist.github.com/miglen/5590986

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM