项目的启动关闭脚本

启动脚本

#!/bin/sh

JAVA_OPTS="-Xms2048m -Xmx2048m -Xmn1024m -XX:MetaspaceSize=1024m -XX:MaxMetaspaceSize=1024m -XX:SurvivorRatio=10 -XX:MaxTenuringThreshold=15 -XX:NewRatio=2 -XX:+DisableExplicitGC"

AppPid=0
LOG_DATE=`date +%Y%m%d`
AppName="AppName"

getAppPid(){
    PID=`ps -ef | grep java | grep ${AppName}-*.jar |awk '{print $2}'`
    if [ -n "$PID" ]; then
        AppPid=`echo $PID | awk '{print $2}'`
    else
        AppPid=0
    fi
}

startup(){
    AppPid=`ps -ef | grep java | grep ${AppName}-*.jar |awk '{print $2}'`
    if [[ $AppPid -ne 0 ]]; then    
        echo "${AppName} already started(PID=$AppPid)!"
        echo 
    else
        echo "Starting ${AppName}..."
        set CLASSPATH=.
        nohup java $JAVA_OPTS -Xloggc:logs/gc.log -jar -Dspring.config.location=application.properties -Dlogging.config=logback.xml ${AppName}-*.jar > logs/server.out 2>&1 &
        AppPid=`ps -ef | grep java | grep ${AppName}-*.jar |awk '{print $2}'`
        if [ $AppPid -ne 0 ]; then
            echo "${AppName} started successful(PID=$AppPid)!"
        else
            echo "${AppName} started  failed!"
        fi
    fi
}

startup

关闭脚本


#!/bin/sh
AppPid=0
AppName="AppName"

getAppPid(){
    javaps=`ps -ef | grep ${AppName}-*.jar | grep -v "$0" | grep -v "grep"`
    if [ -n "$javaps" ]; then
        AppPid=`echo $javaps | awk '{print $2}'`
    else
        AppPid=0
    fi
}

shutdown(){
    getAppPid 
    if [ $AppPid -ne 0 ]; then
        echo "Stopping ${AppName} (PID=$AppPid)..."
        kill -9 $AppPid
        if [ $? -eq 0 ]; then
            echo "${AppName} stopped successful!"
        else
            echo "${AppName} stopped failed!"
        fi
        getAppPid
        if [ $AppPid -ne 0 ]; then
            shutdown
        fi
    else
        echo "${AppName} is not running!"
    fi
}

shutdown
上一篇:Java实现gz压缩与解压缩


下一篇:SpringBoot自注册定时任务