springboot项目怎么在linux服务器上启动、停止脚本

寻技术 Linux / JAVA编程 2023年12月09日 131

本文小编为大家详细介绍“springboot项目怎么在linux服务器上启动、停止脚本”,内容详细,步骤清晰,细节处理妥当,希望这篇“springboot项目怎么在linux服务器上启动、停止脚本”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

    springboot在linux服务器上启动、停止脚本

    xxxx.sh

    #!/bin/bash
    cd `dirname $0`
    CUR_SHELL_DIR=`pwd`
    CUR_SHELL_NAME=`basename ${BASH_SOURCE}`
    #修改这里jar包名即可
    JAR_NAME="xxxxxxxxxxxx.jar" 
    JAR_PATH=$CUR_SHELL_DIR/$JAR_NAME
    #JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m"
    JAVA_MEM_OPTS=""
    #SPRING_PROFILES_ACTIV="-Dspring.profiles.active=eureka2"
    SPRING_PROFILES_ACTIV=""
    LOG_DIR=$CUR_SHELL_DIR/logs
    LOG_PATH=$LOG_DIR/${JAR_NAME}.log
    echo_help()
    {
        echo -e "syntax: sh $CUR_SHELL_NAME start|stop"
    }
    if [ -z $1 ];then
        echo_help
        exit 1
    fi
    if [ ! -d "$LOG_DIR" ];then
        mkdir "$LOG_DIR"
    fi
    if [ ! -f "$LOG_PATH" ];then
        touch "$LOG_DIR"
    fi
    if [ "$1" == "start" ];then
    # check server
        PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
        if [ -n "$PIDS" ]; then
            echo -e "ERROR: The $JAR_NAME already started and the PID is ${PIDS}."
            exit 1
        fi
    echo "Starting the $JAR_NAME..."
        # start
        nohup java $JAVA_MEM_OPTS -jar $SPRING_PROFILES_ACTIV $JAR_PATH >> $LOG_PATH 2>&1 &
        COUNT=0
        while [ $COUNT -lt 1 ]; do
            sleep 1
            COUNT=`ps  --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}' | wc -l`
            if [ $COUNT -gt 0 ]; then
                break
            fi
        done
        PIDS=`ps  --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}'`
        echo "${JAR_NAME} Started and the PID is ${PIDS}."
        echo "You can check the log file in ${LOG_PATH} for details."
    elif [ "$1" == "stop" ];then
        PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
        if [ -z "$PIDS" ]; then
            echo "ERROR:The $JAR_NAME does not started!"
            exit 1
        fi
    echo -e "Stopping the $JAR_NAME..."
        for PID in $PIDS; do
            kill $PID > /dev/null 2>&1
        done
        COUNT=0
        while [ $COUNT -lt 1 ]; do
            sleep 1
            COUNT=1
            for PID in $PIDS ; do
                PID_EXIST=`ps --no-heading -p $PID`
                if [ -n "$PID_EXIST" ]; then
                    COUNT=0
                    break
                fi
            done
        done
        echo -e "${JAR_NAME} Stopped and the PID is ${PIDS}."
    else
        echo_help
        exit 1
    fi

    以上代码只需修改jar包名即可使用

    启动jar包:

     ./xxxx.sh start

    停止 jar包

    ./xxxx.sh  stop

    授权

    chmod +x xxx.sh

    自动化部署之springboot项目快速启动停止脚本

    由于项目需要编写了Spring boot启动停止脚本

    1.脚本需要于jar包放到同一个目录下面

    脚本内容如下:

    #!/bin/bash
    appName=`ls|grep .jar$`
    if [ -z $appName ]
    then
        echo "Please check that this script and your jar-package is in the same directory!"
        exit 1
    fi
    killForceFlag=$2
    function start()
    {
        count=`ps -ef |grep java|grep $appName|wc -l`
        if [ $count != 0 ];then
            echo "Maybe $appName is running, please check it..."
        else
            echo "The $appName is starting..."
            nohup java -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -Xms512M -Xmx4G -jar $appName > /dev/null 2>&1 &
        fi
    }
    function stop()
    {
        appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
        if [ -z $appId ]
        then
            echo "Maybe $appName not running, please check it..."
        else
            echo -n "The $appName is stopping..."
            if [ "$killForceFlag" == "-f" ]
            then 
                echo "by force"
                kill -9 $appId
            else
                echo
                kill $appId
            fi
        fi
    }
    function status()
    {
        appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
        if [ -z $appId ] 
        then
            echo -e "
    关闭

    用微信“扫一扫”