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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值