Linux 启动停止重启 springboot jar包 脚本

初窥门径 -
Linux 启动停止重启 springboot jar包 脚本

#!/bin/bash
#这里可替换为你自己的执行程序,其他代码无需更改
APP_NAME=asug.jar
WAR_LOCATION=../asug/target
BAK_LOCATION=warBak

#使用说明,用来提示输入参数
usage() {
    echo "Usage: sh 脚本名.sh [start|stop|restart|status|deploy]"
    exit 1
}

#检查程序是否在运行
is_exist(){
  pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
  #如果不存在返回1,存在返回0
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}
#启动方法
start(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is already running. pid=${pid} ."
  else
    nohup java  -jar ./${APP_NAME} --server.port=8080 --spring.profiles.active=prod > /dev/null 2>&1 &
  fi
}

#停止方法
stop(){
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
  else
    echo "${APP_NAME} is not running"
  fi
}

#输出运行状态
status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is running. Pid is ${pid}"
  else
    echo "${APP_NAME} is NOT running."
  fi
}

#重启
restart(){
  stop
  start
}

deploy(){
  stop
  cp $APP_NAME $BAK_LOCATION/$APP_NAME.$(date "+%Y%m%d%H%M")
  rm -rf $APP_NAME
  rm -rf nohup.out
  cp $WAR_LOCATION/$APP_NAME $APP_NAME
  start
}

#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  "deploy")
    deploy
    ;;
  *)
    usage
    ;;
esac
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

linuxspringboot

扩展阅读

加个好友,技术交流

1628738909466805.jpg