spring boot jar 包启动通用脚本

spring boot jar 通用脚本

编写原因: 手动敲命令太累了

使用说明:

  • vim filename.sh
  • 复制下面的脚本
  • filename.sh start xxxx.jar
  • 日志在jar 包目录下面 如需修改日志请自行修改
#!/usr/bin/env bash
#
# 
# description: Starts and stops the App.
# author:chen zhan mei
# email:  [email protected]
# createTime: 2019-01-11
 
## java
#export JAVA_HOME=/usr/local/jdk1.8/
#export JRE_HOME=$JAVA_HOME/jre

### 第一个参数
JAR_NAME=$2
### 日志目录 如需修改请自行修改
LOG_PATH=${JAR_NAME%.*}.log
logOutputFormat=`date +%Y-%m-%d__%H:%M:%S`

##检查该对象是否存在
checkPid(){
    pid=`ps -ef |grep ${JAR_NAME} |grep -v grep |awk '{print $2}'`
}

#检查程序是否在运行
status(){
   checkPid
   if [[ ! -n "$pid" ]]; then
        echo "$JAR_NAME is not running $logOutputFormat"
   else
        echo "$JAR_NAME is running  PID  $pid  :execution date  $logOutputFormat"
   fi
}


# 启动脚本
start(){
  checkPid
  if [[ ! -n "$pid" ]]; then
#    nohup java -server -jar $JVM_OPTS ${JAR_NAME} > ${LOG_PATH} 2>&1 &
    nohup java -server -jar  ${JAR_NAME} > ${LOG_PATH} 2>&1 &
    echo "---------------------------------"
    echo "启动完成,按CTRL+C退出日志界面即可>>>>>"
    echo "---------------------------------"
    sleep 3s
    tail -f ${LOG_PATH}
  else
      echo "$JAR_NAME is running PID: $pid"
  fi
}

## 停止脚本
stop(){
    checkPid
    if [ ! -n "$pid" ]; then

     echo "$JAR_NAME not running"
    else

      echo "$JAR_NAME stop..."
      kill ${pid}
    fi
}

## 重启脚本
restart(){
  stop
  start
}

case "$1" in
    status )
        status
        ;;
    start)
        start
        ;;
   stop)
        stop
        ;;
    *)
    echo "Usage: $0 {start|stop|restart|status} + serviceName"
    echo "例子:启动服务 ./start-demo.sh  start $0"
    echo "例子:停止服务 ./start-demo.sh  stop $0"
    echo "例子:重启服务 ./start-demo.sh  restart $0"
    exit 2
esac


你可能感兴趣的:(spring boot jar 包启动通用脚本)