简体   繁体   中英

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 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.

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:

so in your start process:

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

within catalina.sh you will see:

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

here is an example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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