Linux(Centos)下使用shell脚本启动多个tomcat(这里以5个tomcat为例)

==================================================

一、环境说明

(1)tomcat目录: /user/tomcat

(2)tomcat目录下有tomcat1、tomcat2、tomcat3、tomcat4、tomcat5 四个tomcat实例(目录如下图)

注: tomcat实例(目录名)的命名规则: tomcat${i}

在这里插入图片描述


二、编写启动tomcat的shell脚本

(0)tomcat必须使用Linux版本,在Linux中解压完成(使用Window版本需逐个文件授权相应权限,太麻烦)

注: 如下图,“startup.sh”的颜色(XShell连接工具中)为绿色,即为拥有正常权限的文件;Window版的tomcat下,以下文件名颜色全为白色。
在这里插入图片描述

(1)必须在Linux环境中创建startup.sh文件(在window中创建的.sh文件在linux中运行有问题):

[root@localhost tomcat8]# touch startup.sh

(2)授予执行权限

[root@localhost tomcat8]# chmod u+x startup.sh

(3)具体内容(根据后面的附图修改对应项):

#!/bin/bash  
 
# Apache Tomcat daemon  
#  
# chkconfig: 345 10 10  
# description: Apache Tomcat daemon  
#  
# processname: tomcat  

# JDK directory
export JAVA_HOME=/user/jdk8
 
# define variables  
tom="/user/tomcat8/tomcat" # Tomcat directory
startup_bin="bin/startup.sh" 
shutdown_bin="bin/shutdown.sh" 
usage="{p2|p3|p4|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  
fi  
 
#judge $1  
case $1 in  
        "p2")  
                tomcats="1 2" 
                        ;;  
        "p3")  
                tomcats="1 2 3" 
                        ;;  
		"p4")  
                tomcats="1 2 3 4" 
                        ;; 
        "all")  
                tomcats="1 2 3 4 5" 
                        ;;  
        *)  
        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  
        fi  
    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  
        fi  
    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  
        fi  
    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"  
        fi  
    done  
}  
 
#judge $2   
case $2 in  
    "start")  
        tomcatstart  
            ;;  
    "stop")  
        tomcatstop  
            ;;  
    "restart")  
        tomcatrestart  
            ;;  
    "status")  
        tomcatstatus  
            ;;  
    *)  
        echo "Usage: $0 $usage"  
            ;;  
esac

注: 需要按下图说明修改上面脚本对应内容:

在这里插入图片描述

附:查看jdk安装目录

[root@localhost ~]# echo $JAVA_HOME

在这里插入图片描述


三、启动tomcat命令

按以上配置文件中的配置,可使用以下启动命令:

命令提示:

[root@localhost tomcat8]# ./startup.sh
./startup.sh: line 1: pache: command not found

Usage: ./startup.sh {p2|p3|p4|all} {start|stop|restart|status}

[root@localhost tomcat8]# 

(1)启动/停止/重启/状态 tomcat1、tomcat2命令:

[root@localhost tomcat8]# ./startup.sh p2 start
[root@localhost tomcat8]# ./startup.sh p2 stop
[root@localhost tomcat8]# ./startup.sh p2 restart
[root@localhost tomcat8]# ./startup.sh p2 status

(2)启动/停止/重启/状态 tomcat1、tomcat2、tomcat3命令:

[root@localhost tomcat8]# ./startup.sh p3 start
[root@localhost tomcat8]# ./startup.sh p3 stop
[root@localhost tomcat8]# ./startup.sh p3 restart
[root@localhost tomcat8]# ./startup.sh p3 status

(3)启动/停止/重启/状态 tomcat1、tomcat2、tomcat3、tomcat4命令:

[root@localhost tomcat8]# ./startup.sh p4 start
[root@localhost tomcat8]# ./startup.sh p4 stop
[root@localhost tomcat8]# ./startup.sh p4 restart
[root@localhost tomcat8]# ./startup.sh p4 status

(4)启动/停止/重启/状态 tomcat1、tomcat2、tomcat3、tomcat4、tomcat5命令:

[root@localhost tomcat8]# ./startup.sh all start
[root@localhost tomcat8]# ./startup.sh all stop
[root@localhost tomcat8]# ./startup.sh all restart
[root@localhost tomcat8]# ./startup.sh all status

三、开发tomcat防火墙端口

(1)修改防火墙端口

[root@localhost tomcat8]# vi /etc/sysconfig/iptables

在这里插入图片描述

(2)重启防火墙

[root@localhost tomcat8]# service iptables restart

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值