---
title: "shell 管理SpringBoot 生产环境服务-转载"
author: "喧嚣求静"
date: "2018-01-31T15:12:50Z"
tags: []
categories: ["编程语言"]
slug: "shell-springboot"
draft: false
lastmod: "2018-01-31T16:14:36Z"
id: 2409861
url: "https://913.iteye.com/blog/2409861"
---

Spring Boot 在生产环境中运行如果是用maven 打包为jar 运行那么再关闭 重启服务这样每次在操作的时候就非常不方便 这里写了个shell 脚本统一管理

```
# description: Auto-starts boot

Tag="PublishImageApplication"
MainClass="com.yoke.PublishImageApplication"
Lib="/test/lib/"
Log="/test/run.log"
echo $Tag
RETVAL="0"

# See how we were called.
function start() {
    echo  $Log 
    if [ ! -f $Log ]; then
        touch $Log
    fi
    nohup java -Dappliction=$Tag -Djava.ext.dirs=$Lib":${JAVA_HOME}/jre/lib/ext" $MainClass > $Log 2>&1 &  
    tailf $Log
}


function stop() {
    pid=$(ps -ef | grep -v 'grep' | egrep $Tag| awk '{printf $2 " "}')
    if [ "$pid" != "" ]; then      
        echo -n "boot ( pid $pid) is running" 
        echo 
        echo -n $"Shutting down boot: "
        pid=$(ps -ef | grep -v 'grep' | egrep $Tag| awk '{printf $2 " "}')
        if [ "$pid" != "" ]; then
            echo "kill boot process"
            kill -9 "$pid"
        fi
        else 
             echo "boot is stopped" 
        fi

    status
}

function status()
{
    pid=$(ps -ef | grep -v 'grep' | egrep $Tag| awk '{printf $2 " "}')
    #echo "$pid"
    if [ "$pid" != "" ]; then
        echo "boot is running,pid is $pid"
    else
        echo "boot is stopped"
    fi
}



function usage()
{
   echo "Usage: $0 {start|stop|restart|status}"
   RETVAL="2"
}

# See how we were called.
RETVAL="0"
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    reload)
        RETVAL="3"
        ;;
    status)
        status
        ;;
    *)
      usage
      ;;
esac

exit $RETVAL
```

 Tag： 在中起着标识程序运行标志，如果服务器运行多个java 程序需用java 的 -Dappliction 来区分对应程序

MainClass： 是对应程序的运行的SpringBoot main 类

Lib：是boot程序在生产服务器环境中的所有jar 路径

Log：是记录boot程序运行所有日志保存路径

如脚本在   
/boot/run\_boot.sh   
启动命令： /boot/run\_boot.sh start   
重启命令： /boot/run\_boot.sh restart   
关闭命令： /boot/run\_boot.sh stop   
是否运行： /boot/run\_boot.sh status

文章来源：[http://blog.csdn.net/jiangzeyin\_/article/details/74671334](http://blog.csdn.net/jiangzeyin_/article/details/74671334)