分享一个Spring Boot Jar包启动停止脚本

395次阅读
没有评论

共计 1711 个字符,预计需要花费 5 分钟才能阅读完成。

我们都知道Jar启动命令java -jar jar包,但是窗口关闭,项目就不能访问了;
如果需要后台运行就要用nohup java -jar jar包 &,这样程序就会在后台启动,并且不会收到命令窗口关闭的影响;
但是这样每次都输命令,也还是不方便,下面脚本可以帮助你只需简单输入start、stop、status可以部署,停止,状态;

脚本内容:

#!/bin/bash
#这里可替换为你自己的执行程序,其他代码无需更改
APP1_NAME=jar1包路径
APP2_NAME=jar2包路径

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

#检查程序是否在运行
is_exist_admin() { 
    pid=`ps -ef | grep $APP1_NAME | grep -v grep | awk '{print $2}' `
    #如果不存在返回1,存在返回0
    if [ -z "${pid}" ]; then
      return 1
    else
      return 0
    fi
}
is_exist_web() { 
    pid=`ps -ef | grep $APP2_NAME | grep -v grep | awk '{print $2}' `
    #如果不存在返回1,存在返回0
    if [ -z "${pid}" ]; then
      return 1
    else
      return 0
    fi
}

#启动方法
start() {
   is_exist_admin
   if [ $? -eq "0" ]; then
    # java -jar ${APP1_NAME}
     echo "${APP1_NAME} is already running. pid=${pid} ."
   else
     nohup java -jar $APP1_NAME > /dev/null 2>&1 &
   fi

   is_exist_web
   if [ $? -eq "0" ]; then
    # java -jar ${APP2_NAME}
     echo "${APP2_NAME} is already running. pid=${pid} ."
   else
     nohup java -jar $APP2_NAME > /dev/null 2>&1 &
   fi
}

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

   is_exist_web
   if [ $? -eq "0" ]; then
     kill -9 $pid
   else
     echo "${APP2_NAME} is not running"
   fi
}

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

   is_exist_web
   if [ $? -eq "0" ]; then
     echo "${APP2_NAME} is running. Pid is ${pid}"
   else
     echo "${APP2_NAME} is not running."
   fi
}

#重启
restart() {
   stop
   start
}

#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
   "start")
     start
     ;;
   "stop")
     stop
     ;;
   "status")
     status
     ;;
   "restart")
     restart
     ;;
   *)
     usage
     ;;
esac

添加快捷命令

# root目录
cd /root
vim .bashrc
# 添加以下配置后保存退出
alias start='cd 脚本所在目录; sh myBlogService start'
alias status='cd 脚本所在目录; sh myBlogService status'
alias stop='cd 脚本所在目录; sh myBlogService stop'
alias restart='cd 脚本所在目录; sh myBlogService restart'
# 刷新生效
source .bashrc

使用快捷命令

接下就可以通过简单的start、stop、status、restart,让你的项目部署更加便利;
start 启动
stop 停止
restart 重启
status 程序状态

正文完
 
裴先生
版权声明:本站原创文章,由 裴先生 2021-02-11发表,共计1711字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
本站勉强运行: