SpringBoot 项目在Linux服务器上启动、停止脚本2

SpringBoot 项目在Linux服务器上启动、停止脚本2

创建脚本

在jar包目录,创建脚本

vim springboot.sh

脚本内容

#!/bin/bash
cd `dirname $0`
CUR_SHELL_DIR=`pwd`
CUR_SHELL_NAME=`basename ${BASH_SOURCE}`
#JAVA_MEM_OPTS=" -server -Xms1g -Xmx1g -XX:PermSize=128m"
JAVA_MEM_OPTS=" -server -Xms512m -Xmx512m -XX:PermSize=128m"
#SPRING_PROFILES_ACTIV="-Dspring.profiles.active=dev"
SPRING_PROFILES_ACTIV=""
LOG_DIR=$CUR_SHELL_DIR/logs
LOG_PATH=$LOG_DIR/${JAR_NAME}.log


## 第3个参数为应用名,如果为空,则取当前项目下的*.jar第一个.jar包
appName=$2
if [ -z $appName ];then
    appName=`ls -t |grep .jar$ |head -n1`
fi

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 $JAVA_MEM_OPTS -jar $SPRING_PROFILES_ACTIV ./$appName >> $LOG_PATH 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 "The $appName is stopping..."
        kill $appId
	fi
}

function restart()
{
    # get release version
    releaseApp=`ls -t |grep .jar$ |head -n1`
    
    # get last version 
    lastVersionApp=`ls -t |grep .jar$ |head -n2 |tail -n1`

    appName=$lastVersionApp
    stop
    for i in {5..1}
    do
        echo -n "$i "
        sleep 1
    done
    echo 0
    
    backup
    
    appName=$releaseApp
    start
}

function backup() 
{
    # get backup version
    backupApp=`ls |grep -wv $releaseApp$ |grep .jar$`
    
    # create backup dir
    if [ ! -d "backup" ];then
        mkdir backup
    fi
    
    # backup
    for i in ${backupApp[@]}
    do
        echo "backup" $i
        mv $i backup
    done
}

function status()
{
    appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
	if [ -z $appId ] 
	then
	    echo -e "\033[31m Not running \033[0m" 
	else
	    echo -e "\033[32m Running [$appId] \033[0m" 
	fi
}


function usage()
{
    echo "Usage: $0 {start|stop|restart|status|stop -f}"
    echo "Example: $0 start"
    exit 1
}

case $1 in
	start)
	start;;

	stop)
	stop;;
	
	restart)
	restart;;
	
	status)
	status;;
	# 其它命令输入,调用usage函数
	*)
	usage;;
esac

增加可执行权限

chmod +x xxxxxx.sh

使用

启动jar包

 ./springboot.sh start xxx.jar

停止 jar包

./springboot.sh  stop xxx.jar

重启 jar包

./springboot.sh  restart xxx.jar

查看状态

./springboot.sh status xxx.jar

查看帮助

./springboot.sh help xxx.jar

你可能感兴趣的:(Spring,Boot)