How to write a project deployment script based on Linux system

原创
02/07 17:27
阅读数 4

How to analyze the project deployment script

overview

lwk@qwfys:~$ tree.deploy/script/bin
.deploy/script/bin
├── debug.sh
├── redebug.sh
├── restart.sh
├── run.sh
├── run.sh.bak
├── start.bat
├── start.sh
├── status.sh
└── stop.sh

run.sh

#!/bin/bash
#
# springboot project startup script
#


SERVICE_PATH=$(cd `dirname $0`; pwd)
PARENT_PATH=$(cd `dirname $0`; cd ..;pwd)

MODULE_IMPL_NAME=${SERVICE_PATH##*/}
MODULE_NAME=${MODULE_IMPL_NAME%-impl*}
EXECUTE_JAR_NAME=$MODULE_IMPL_NAME.jar

EXECUTE_JAR_PATH=$SERVICE_PATH/$EXECUTE_JAR_NAME
DEFAULT_LOGGER_ROOT=/logs/dubbo-jars

JAVA_VERSION=1.8

getpid() {
    #pid=`pgrep -f "java.*$PROJECT"`
    pid=`ps -ef | grep "$MODULE_IMPL_NAME" | grep -v "$0" | grep -v "grep" |  awk '{print $2}'`
}

start() {
    getpid
    if [ -n "$pid" ]; then
        echo "$MODULE_NAME (pid $pid) is already running"
        exit 1
    fi

    if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
        JAVA_CMD="$JAVA_HOME/bin/java"
    elif [[ -n "$JAVA" ]] && [[ -x "$JAVA" ]];  then
        JAVA_CMD="$JAVA"
    else
        JAVA_CMD=java
    fi

    if [[ "$JAVA_CMD" ]]; then
        version=$("$JAVA_CMD" -version 2>&1 | awk -F '"' '/version/ {print $2}')
        if [[ "$version" < $JAVA_VERSION ]]; then
            echo JAVA_CMD=$JAVA_CMD
            echo Java version "$version" is less than required $JAVA_VERSION
            exit 1
        fi
    fi

    cd "$SERVICE_PATH"

    "$JAVA_CMD" -jar -Xdebug -Xnoagent -Xmx1024m -Xms1024m -XX:NewRatio=1 -XX:SurvivorRatio=8 -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=0.0.0.0:29000  -Dlogger.root="$DEFAULT_LOGGER_ROOT" -Dlogger.module="$MODULE_NAME" "$EXECUTE_JAR_PATH" --spring.profiles.active=beta >> "$SERVICE_PATH"/out.nohup &

    echo -ne "Starting process"
    for i in {1..10}; do
        if ! [ -n "$pid" ]; then
            echo -ne "."
            sleep 1
            getpid
        fi
    done
    echo

    if [ -n "$pid" ]; then 
        status
    else 
        echo "Error during $MODULE_IMPL_NAME starting, see log for details."
    fi
}

stop() {
    status
    if [ -n "$pid" ]; then
        echo -ne "Stopping process"
        kill -9 $pid
        res=$?
        for i in {1..10}; do
            if [ -n "$pid" ]; then
                echo -ne "."
                sleep 1
                getpid
            fi
        done
        echo
        if ! [ -n "$pid" ]; then 
            echo "$MODULE_IMPL_NAME has been successfully stopped."
        else 
            echo "Error during $MODULE_IMPL_NAME stopping... $res"
        fi
    fi
}

status() {
    getpid
    if [ -n "$pid" ]; then 
        echo "$MODULE_IMPL_NAME (pid $pid) is running..."
    else 
        echo "$MODULE_IMPL_NAME is NOT running"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit 0
root@vm67:~ #

debug.sh

 #!/bin/sh
basepath=$(cd `dirname $0`; pwd)
sh "$basepath"/run.sh debug

redebug.sh

basepath=$(cd `dirname $0`; pwd)
sh "$basepath"/run.sh redebug

restart.sh

basepath=$(cd `dirname $0`; pwd)
sh "$basepath"/run.sh restart

start.sh

 #!/bin/sh
basepath=$(cd `dirname $0`; pwd)
sh "$basepath"/run.sh start

status.sh

#!/bin/sh
basepath=$(cd `dirname $0`; pwd)
sh "$basepath"/run.sh status

stop.sh

#!/bin/sh
basepath=$(cd `dirname $0`; pwd)
sh "$basepath"/run.sh stop
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部