Linux Shell starts multiple Tomcat

Sometimes multiple application servers are used for load balancing when deploying a project. Here, multiple Tomcats are used for load.

Server Environment: CentOS 6.5

Tomcat directory: /usr/local

There are four tomcat instances in the Tomcat directory: tomcat1, tomcat2, tomcat3, and tomcat4.

Naming rules for tomcat instances: tomcat${i}

 

1. Create a new shell file named tomcat.sh

#!/bin/bash  
 
# Apache Tomcat daemon  
#  
# chkconfig: 345 10 10  
# description: Apache Tomcat daemon  
#  
# processname: tomcat  
 
export JAVA_HOME=/usr/local/java/jdk1.7.0_80
 
#define variables  
tom="/usr/local/tomcat"
startup_bin="bin/startup.sh"
shutdown_bin="bin/shutdown.sh"
usage="{p2|p3|all} {start|stop|restart|status}"
dev="/dev/null"
 
#judge $1 $2 whether null  
if [ "$1" == "" -o "$2" == "" ];then  
    echo  
    echo "Usage: $0 $usage"  
    echo   
    exit 1  
be  
 
#judge $1  
case $1 in  
        "p3")  
                tomcats="1 2 3"
                        ;;  
        "p2")  
                tomcats="1 2"
                        ;;  
        "all")  
                tomcats="1 2 3 4"
                        ;;  
        *)  
        echo "Usage: $0 $usage"  
            ;;  
esac  
 
#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  
        be  
    done  
}  
 
#define stop function  
tomcatstop() {  
    for j in $tomcats  
    do  
        tom1_home="$tom$j"
        run1_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom1_home}")  
        if [ "${run1_status}X" == "X" ];then  
            echo "tomcat$j is not running..."  
        else  
            ${tom1_home}/${shutdown_bin} &>$dev  
            echo "tomcat$j stopping,Please wait 2s..."  
            sleep 2  
        be  
    done  
}  
 
#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  
            ${tom2_home}/${shutdown_bin} &>$dev  
            echo "tomcat$m stopping,Please wait 2s..."  
            sleep 2  
            ${tom2_home}/${startup_bin} &>$dev  
            echo "tomcat$m starting,Please wait 2s..."  
            sleep 2  
        be  
    done  
}  
 
#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"  
        be  
    done  
}  
 
#judge $2   
case $2 in  
    "start")  
        tomcatstart  
            ;;  
    "stop")  
        tomcatstop  
            ;;  
    "restart")  
        tomcatrestart  
            ;;  
    "status")  
        tomcatstatus  
            ;;  
    *)  
        echo "Usage: $0 $usage"  
            ;;  
esac  

 

2. Modify the permissions of the file tomcat.sh and change it to executable

chmod +x tomcat.sh

 

3. Start the tomcat command

For example, if you want to start two instances of tomcat1 and tomcat2:

./tomcat.sh  p2  start

 

stop command:

./tomcat.sh  p2 stop

 

4. Configure the tomcat script as a system service

cp tomcat.sh /etc/init.d/tomcat
sudo chkconfig --add /etc/init.d/tomcat

 Check if it has been added to the service list

chkconfig --list

 

 After adding to the service, you can start, stop, and restart tomcat as a service.

service tomcat p2 start

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326305897&siteId=291194637