半步多 玄玉的博客

发布Java应用的Shell脚本

2015-06-03
玄玉

本文演示的脚本,依赖的环境和工具有:linux、jdk、tomcat、maven、git/svn

主要包括功能:自动从git/svn仓库拉取最新代码至临时目录、maven编译打包、重发布到tomcat、删除临时文件、最后重启tomcat

关于脚本中一些命令的含义,可参考我之前的博文:https://jadyer.cn/2012/09/09/linux-shell-java/

deploy-engine

下面是包含了核心逻辑的,具体的脚本内容deploy-engine.sh

该脚步可放置于服务器任何目录下执行,不过我的习惯是统一放到 /tomcat/bin/ 目录下

#!/bin/sh
# 部署在/tomcat/webapps/目录下的应用名
APP_NAME=ROOT
# tomcat目录的绝对路径
APP_PATH=/app/tomcat-6.0.43
# tomcat里面存储临时代码的文件夹名称(发布完会自动删除)
APP_CODE=sourcecode
# SVN_URL=https://svn.sinaapp.com/jadyer/2/repository/seed
# SVN_USER=jadyer@yeah.net
# SVN_PSWD=xuanyu
# git仓库地址、应用名称、打成的war所在的模块名称
GIT_URL=https://jadyer%40yeah.net:xuanyu@github.com/jadyer/seed.git
GIT_CODE=seed
GIT_MODULE=seed-mpp

appPID=0
getAppPID(){
    pidInfo=`ps aux|grep java|grep $APP_PATH|grep -v grep`
    if [ -n "$pidInfo" ]; then
        appPID=`echo $pidInfo | awk '{print $2}'`
    else
        appPID=0
    fi
}

downloadAndCompileSourceCode(){
    cd $APP_PATH
    mkdir $APP_CODE
    # svn --username $SVN_USER --password $SVN_PSWD checkout $SVN_URL $APP_CODE
    cd $APP_CODE
    git clone $GIT_URL --depth=2
    cd $GIT_CODE
    mvn clean package -DskipTests
}

shutdown(){
    getAppPID
    echo "[玄玉] =========================================================================================="
    if [ $appPID -ne 0 ]; then
        echo -n "[玄玉] Stopping $APP_PATH(PID=$appPID)..."
        kill -9 $appPID
        if [ $? -eq 0 ]; then
            echo "[Success]"
            echo "[玄玉] =========================================================================================="
        else
            echo "[Failed]"
            echo "[玄玉] =========================================================================================="
        fi
        getAppPID
        if [ $appPID -ne 0 ]; then
            shutdown
        fi
    else
        echo "[玄玉] $APP_PATH is not running"
        echo "[玄玉] =========================================================================================="
    fi
}

deploy(){
    cd $APP_PATH/webapps/
    rm -rf $APP_NAME
    rm -rf $APP_NAME.war
    cp $APP_PATH/$APP_CODE/$GIT_CODE/$GIT_MODULE/target/*.war $APP_NAME.war
    cd $APP_PATH/logs/
    rm -rf *
    cd $APP_PATH
    rm -rf $APP_CODE
}

startup(){
    cd $APP_PATH/bin
    ./startup.sh
    tail -100f ../logs/catalina.out
}

downloadAndCompileSourceCode
shutdown
deploy
startup

中途退出的问题

上面的脚本在使用命令sh deploy-engine.sh直接执行的过程中,若未执行完毕便Ctrl+C退出,这会导致应用部署失败

所以,可以使用命令nohup ./deploy-engine.sh &来执行,这样中途Ctrl+C就不会有问题了

deploy

这是额外提供的、基于上面的deploy-engine.sh基础上的额外封装、可直接执行的脚本deploy.sh

而即便执行过程中,Ctrl+C退出了,不也会有什么影响,应用会后台继续自动部署

该脚本同样可放置于服务器任何目录下执行

#!/bin/sh
# 执行真正逻辑的启动脚本文件名(默认会去/tomcat/bin/下取该文件)
SHELL_NAME=deploy-engine.sh
# tomcat目录
APP_PATH=/app/tomcat-6.0.43

shellPID=0
getShellPID(){
    pidInfo=`ps aux|grep $SHELL_NAME|grep -v grep`
    if [ -n "$pidInfo" ]; then
        shellPID=`echo $pidInfo | awk '{print $2}'`
    else
        shellPID=0
    fi
}

shutdown(){
    getShellPID
    echo "[玄玉] =========================================================================================="
    if [ $shellPID -ne 0 ]; then
        echo -n "[玄玉] Stopping $SHELL_NAME(PID=$shellPID)..."
        kill -9 $shellPID
        if [ $? -eq 0 ]; then
            echo "[Success]"
            echo "[玄玉] =========================================================================================="
        else
            echo "[Failed]"
            echo "[玄玉] =========================================================================================="
        fi
        getShellPID
        if [ $shellPID -ne 0 ]; then
            shutdown
        fi
    else
        echo "[玄玉] $SHELL_NAME is not running"
        echo "[玄玉] =========================================================================================="
    fi
}

# [2>&1]表示把标准错误(stderr)重定向到标准输出(stdout),否则会提示[nohup: redirecting stderr to stdout]
startupByNohup(){
    cd $APP_PATH/logs
    rm -rf nohup.log
    nohup ../bin/$SHELL_NAME > nohup.log 2>&1 &
    sleep 1
    tail -100f nohup.log
}

shutdown
startupByNohup

Comments

Content