linux下安装、卸载Tomcat脚本-极客文库-知识库

如果图片无法查看或格式错乱,请前往极客文库-知识库查看原文

linux下安装Tomcat脚本

#!/bin/bash

base_dir=$(cd "$(dirname "$0")";pwd)

JDK_FILE=$(ls | grep jdk-*-linux-*.tar.gz)
TOMCAT_FILE=$(ls | grep apache-tomcat-*.tar.gz)
#下载JDK
download(){
    os_version=`uname -a`
    echo $os_version
    architecture="64"
    echo "$os_version" | grep -q "$architecture"

    if [ $? -eq 0 ]
    then
        # 不存在即去外网下载jdk文件
        if [ ! -f "$JDK_FILE" ]; then
            echo "您正在使用64位操作系统,为您选择64位JDK"
            wget http://7xls9k.dl1.z0.glb.clouddn.com/jdk-8u60-linux-x64.tar.gz
        fi
    else
        # 不存在即去外网下载jdk文件
        if [ ! -f "$JDK_FILE" ]; then
            echo "您正在使用32位操作系统,为您选择32位JDK"
            wget http://7xls9k.dl1.z0.glb.clouddn.com/jdk-8u60-linux-i586.tar.gz
        fi
    fi
    #下载tomcat
    if [ ! -f "$TOMCAT_FILE" ]; then
        wget http://7xls9k.dl1.z0.glb.clouddn.com/apache-tomcat-8.0.26.tar.gz
    fi
    JDK_FILE=$(ls | grep jdk-*-linux-*.tar.gz)
    TOMCAT_FILE=$(ls | grep apache-tomcat-*.tar.gz)
}

#安装JDK
install_jdk(){
    JAVA_DIR=/usr/local/java
    JDK_DIR="jdk1.8.0_60"
    JDK_PATH="$JAVA_DIR"/"$JDK_DIR"

    tar xzf $JDK_FILE

    mkdir -p $JAVA_DIR
    mv $JDK_DIR  $JAVA_DIR
    #配置环境变量
    cp ~/.bashrc ~/.bashrc.backup.java
    if [ ! -n "$JAVA_HOME" ]; then
        echo "export JAVA_HOME=\"$JDK_PATH\"" >> ~/.bashrc
    fi
    if [ ! -n "$JRE_HOME" ]; then
        echo "export JRE_HOME=\"\$JAVA_HOME/jre\"" >> ~/.bashrc
    fi   
    if [ ! -n "$CLASSPATH" ]; then
        echo "export CLASSPATH=.:\$JDK_PATH/lib/dt.jar:\$JDK_PATH/lib/tools.jar" >> ~/.bashrc
    fi
    echo "export PATH=\$JAVA_HOME/bin:\$JRE_HOME/bin:\$PATH" >> ~/.bashrc
    source ~/.bashrc
    echo "JDK install success!"
}
#安装tomcat
install_tomcat(){
    TOMCAT_DIR=/usr/local/tomcat8

    mkdir -p $TOMCAT_DIR

    tar xzf $TOMCAT_FILE
    mv apache-tomcat-8.0.26 tomcat8
    mv tomcat8 /usr/local/

    cp ~/.bashrc ~/.bashrc.backup.tomcat8
    if [ ! -n "$TOMCAT_HOME" ]; then
        echo "export TOMCAT_HOME=$TOMCAT_DIR" >> ~/.bashrc
    fi
    if [ ! -n "$CATALINA_HOME" ]; then
        echo "export CATALINA_HOME=$TOMCAT_DIR" >> ~/.bashrc
    fi
    source ~/.bashrc
    echo "Tomact install success!"
}

main(){
    download
    if [ $? != 0 ]; then
        echo "tomcat & JDK download  failed"
        exit 1
    fi
    install_jdk
    if [ $? != 0 ]; then
        echo "JDK install failed"
        exit 1
    fi
    install_tomcat
    if [ $? != 0 ]; then
        echo "Tomcat install failed"
        exit 1
    fi
}
main

Linux下卸载Tomcat脚本

#!/bin/bash


#卸载JDK
uninstall_jdk(){
    JAVA_DIR=/usr/local/java/jdk1.8.0_60
    TOMCAT_DIR=/usr/local/tomcat8

    if [ -d "$JAVA_DIR" ]; then
        rm -rf $JAVA_DIR
    fi

    if [ -d "$TOMCAT_DIR" ]; then
        rm -rf $TOMCAT_DIR
    fi

    #环境变量
    if [ -f "~/.bashrc.backup.tomcat8" ]; then
        mv ~/.bashrc.backup.tomcat8 ~/.bashrc
    fi

    if [ -f "~/.bashrc.backup.java" ]; then
        mv ~/.bashrc.backup.java ~/.bashrc
    fi
    source ~/.bashrc
    echo "JDK,Tomcat uninstall success!"
    cd
}

main(){
    uninstall_jdk
    if [ $? != 0 ]; then
        echo "JDK,Tomcat uninstall failed"
        exit 1
    fi
}
main
#!/bin/bash  

# Source function library.  
. /etc/rc.d/init.d/functions  

export JAVA_HOME=
export JRE_HOME=

# tomcat名字
tcName=tomcat-$1
basedir=/data/program/tomcat/$tcName
tclog=${basedir}/logs/catalina.out

RETVAL=0  

start(){ 
        checkrun  
        if [ $RETVAL -eq 0 ]; then  
                echo "###### Tomcat正在启动 ######"  
                $basedir/bin/startup.sh  
                touch /var/lock/subsys/${tcNo}
                checklog 
                status
        else  
                echo "###### Tomcat启动成功 ######"  
        fi  
}  

# 停止某一台tomcat,如果是重启则带re参数,表示不查看日志,等待启动时再提示查看  
stop(){
        checkrun  
        if [ $RETVAL -eq 1 ]; then  
                echo "###### Tomcat正在关闭 ######"  
                $basedir/bin/shutdown.sh  
                if [ "$1" != "re" ]; then
                  checklog
                else
                  sleep 5
                fi
                rm -f /var/lock/subsys/${tcNo} 
                status
        else  
                echo "###### Tomcat关闭成功 ######"  
        fi  
}  

status(){
        checkrun
        if [ $RETVAL -eq 1 ]; then
                echo -n "-- Tomcat ( pid "  
                ps ax --width=1000 |grep ${tcName}|grep "org.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}'
                echo -n ") 正在运行"  
                echo  
        else
                echo "###### Tomcat未运行 ######"  
        fi
        #echo "---------------------------------------------"  
}

# 查看tomcat日志,带vl参数
log(){
        status
        checklog yes
}

# 如果tomcat正在运行,强行杀死tomcat进程,关闭tomcat
kill(){
        checkrun
        if [ $RETVAL -eq 1 ]; then
            read -p "###### 确定要杀死 ${tcName} 的进程吗?[no])" answer
            case $answer in
                Y|y|YES|yes|Yes)
                    ps ax --width=1000 |grep ${tcName}|grep "org.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}'|xargs kill -9  
                    status
                ;;
                *);;
            esac
        else
            echo "###### 退出 [$tcName] ######"
        fi
}


checkrun(){  
        ps ax --width=1000 |grep ${tcName}| grep "[o]rg.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}' | wc | awk '{print $2}' >/tmp/tomcat_process_count.txt  
        read line < /tmp/tomcat_process_count.txt  
        if [ $line -gt 0 ]; then  
                RETVAL=1  
                return $RETVAL  
        else  
                RETVAL=0  
                return $RETVAL  
        fi  
}  

# 如果是直接查看日志viewlog,则不提示输入[yes],否则就是被stop和start调用,需提示是否查看日志
checklog(){
        answer=$1
        if [ "$answer" != "yes" ]; then
            read -p "###### 查看 <catalina.out> 日志吗 $2?[yes])" answer
        fi
        case $answer in
            Y|y|YES|yes|Yes|"")
                tail -f ${tclog}
            ;;
            *)
            #    status
            #    exit 0
            ;;
        esac
}
checkexist(){
        if [ ! -d $basedir ]; then
            echo "###### tomcat $basedir 不存在 ######"
            exit 0
        fi
}


case "$2" in  
start)  
        checkexist
        start  
        exit 0
        ;;  
stop)  
        checkexist
        stop  
        exit 0
        ;;  
restart)  
        checkexist
        stop re 
        start 
        exit 0
        ;;  
status)  
        checkexist
        status  
        #$basedir/bin/catalina.sh version  
        exit 0
        ;;  
log)
        checkexist
        log
        exit 0
        ;;
kill)
        checkexist
        status
        kill
        exit 0
        ;;
*)  
        echo "###### 使用方法: service $0 [start|stop|restart|status|log|kill]"  
        echo "###### 举个栗子-> service tomcat xintr start"
        esac  

exit 0
本站所有文章均来自互联网,如有侵权,请联系站长删除。极客文库 » linux下安装、卸载Tomcat脚本-极客文库-知识库
分享到:
赞(0)

评论抢沙发

评论前必须登录!