springboot之jar运行脚本

  一、现在的工程都将就独立和简单了,我们在使用springboot做开发或者其他框架做开发时,在linux上面执行的时候。总会写一下脚本,目的当然是为了更加好的运行程序。不然每次都手动输入一下命令,来调试环境。调整端口等。个人感觉特别麻烦,那么问题来了。为了更好的让项目运行并且看到其中的状态,所以一个好的脚本是非常重要的。

  二、这里个人记录一下自己常使用的jar启动脚本

  

#! /bin/sh
projectName="test"

#提醒功能
help() {
    echo "help: sh ${projectName}.sh [start|stop|restart]"
    exit 1
}

#判断项目是否运行,并且返回对应数字(0 为未启动 1为启动)
is_exist(){
    #获取pid的方式,个人喜欢咋写就咋写
    pid=`ps -ef|grep ${projectName}.jar|grep -v grep|awk '{print $2}'`
    if [ -z "${pid}" ]
    then
        return 0
    else
        return 1
    fi
}

#开始运行项目
start(){
    is_exist
    #如果正在运行直接提示
    if [ $? -eq 1 ]
    then
        echo "${projectName} is running"
        exit 0;
    else
        #没有运行则运行项目
        echo "start running ${projectName} ..."
        currentPath=`pwd`
        startPath=$(cd `dirname $0`;pwd)
        #这里写的比较简单,实际生产环境,需要配置参数
        cd ${startPath}
        nohup java -jar -server ${projectName}.jar > ${projectName}/run.log 2>&1 &
        cd ${currentPath}
    fi
}

#停止项目
stop(){
    echo "stop $projectName ..."
    is_exist
    if [ $? -eq 1 ]
    then
        #通过pid关闭
        kill -9 $pid
        echo  "[ok]"
    else
        echo  "[ok]"
    fi
}

//选择运行方式
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    stop
    start
    ;;
*)
    help
    ;;
esac

  说明:if中的[]必须加空格

原文地址:https://www.cnblogs.com/ll409546297/p/10536917.html