springboot项目(jar包)启动、重启、停止命令脚本封装,以及进程自动重启配置

本文介绍了一个用于自动化部署Java应用程序的Shell脚本。该脚本能自动查找并启动最新的jar包,并支持启动、停止、重启和状态查询等操作。此外,还介绍了如何通过设置定时任务实现程序崩溃后的自动重启。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 使用须知

  1. 需要将当前脚本放到jar包所在的目录;
  2. 默认执行当前脚本所在目录中最新的jar(☆)
  3. 需要修改脚本中的JAVA_HOME为对应自己服务器上的路径
  4. 根据需要调整执行java程序的内存相关参数(在脚本中java -jar -Xms512m ....
  5. 根据需要调整执行spring的profile(在脚本中spring.profiles.active=test
  6. 下面脚本中增了端口的判断,如果不需要则把 #if [ -z "${pid}" ]; then打开,注释它的下一行。
#!/bin/sh
export JAVA_HOME=/opt/app/jdk1.8.0_152
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

#模糊查询最新打包的jar文件名
APP_NAME=`ls -lt  *.jar | head -n 1 |awk '{print $9}'`
echo -e "The current app name is:\033[35m$APP_NAME\033[0m"

#pid=`netstat -anp|grep 8085|awk '{printf $7}'|cut -d/ -f1`
#pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`


#检查程序是否在运i行

is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
port=`netstat -ntlp | grep " $pid/"|awk '{print $4}'|awk -F '[:]' '{print $NF}' | awk '{a=a$1"  "}END{print a}'` 
 #如果不存在返回1,存在返回0     
  #if [ -z "${pid}" ]; then
  if [ -z "${pid}" -o -z "${port}" ]; then
   return 1
  else
    return 0
  fi
}
#启动方法
start(){
  is_exist
  if [ $? -eq 0 ]; then
    echo "${APP_NAME} is already running. pid is ${pid}, port is ${port}"
  else
    nohup ${JAVA_HOME}/bin/java -jar -Xms512m -Xmx512m -Xmn256m -Xss228k -XX:MaxMetaspaceSize=512m ${APP_NAME} --spring.profiles.active=test >> nohup.out 2>&1 &
    echo "${APP_NAME} is starting"
    while true; do
      is_exist
      if [ $? -eq 0 ]; then
	  echo -e  "\n${APP_NAME} is start completed. pid is ${pid}, port is ${port}"
	  break;
      else
	  echo -e ".\c"
	  sleep 0.02
      fi
    done 
  fi
}
 
#停止方法
stop(){
  is_exist
  if [ $? -eq "0" ]; then
    kill -15 $pid
    echo "${APP_NAME} is closing. pid is $pid, port is $port"
    while true; do
      is_exist
      if [ $? -eq "1" ]; then
	 echo -e "\n${APP_NAME} is shutdown"
         break;
      else
	 echo -e ".\c"
	 sleep 0.02
      fi
    done      
  else
    echo "${APP_NAME} is not running"
  fi  
}
 
#输出运行状态
status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is running. pid is ${pid}, port is ${port}"
  else
    echo "${APP_NAME} is not running."
  fi
}
 
#重启
restart(){
  stop
  sleep 1
  start
}

#帮助
usage(){
  echo -e "参数输入错误:\033[31m$1\033[0m , 支持以下参数:"
  echo -e "\033[36mstart\033[0m\t:启动程序"
  echo -e "\033[36mstop\033[0m\t:停止程序"
  echo -e "\033[36mrestart\033[0m\t:重启程序"
  echo -e "\033[36mstatus\033[0m\t:查看程序运行状态"
}

case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  *)
    usage $1
    ;;
esac

将上面的脚本保存为 app.sh,同时赋予可执行权限chmod u+x app.sh

2. 使用举例

  1. 切换到jar包所在目录

    [root@wuwei app]# cd /opt/app
    [root@wuwei app]# ll
    total 419384
    -rwxr--r-- 1 root root      1729 Sep 18 10:37 app.sh
    -rw-r--r-- 1 root root      3690 Sep 18 10:54 autorun.log
    -rwxr--r-- 1 root root       195 Sep 18 10:54 autorun.sh
    -rw-r--r-- 1 root root  39877279 Feb 24  2021 exhibition-1.0-SNAPSHOT.jar
    -rw-r--r-- 1 root root  39877296 Mar  3  2021 exhibition-1.1-SNAPSHOT.jar
    -rw-r--r-- 1 root root  39877301 Mar  3  2021 exhibition-1.2-SNAPSHOT.jar
    -rw-r--r-- 1 root root  39878242 Mar  7  2021 exhibition-1.3-SNAPSHOT.jar
    -rw-r--r-- 1 root root  39883196 Mar 11  2021 exhibition-1.4-SNAPSHOT.jar
    -rw-r--r-- 1 root root  39883049 Mar 12  2021 exhibition-1.5-SNAPSHOT.jar
    drwxr-xr-x 8 1001 1001       277 Sep  4  2020 jdk1.8.0_152
    -rw------- 1 root root    155492 Sep 18 10:41 nohup.out
    
  2. 重启

    [root@iccboy app]# ./app.sh restart
    The current app name is:exhibition-1.5-SNAPSHOT.jar
    exhibition-1.5-SNAPSHOT.jar is closing. pid is 184518, port is 80  443
    ..
    exhibition-1.5-SNAPSHOT.jar is shutdown
    exhibition-1.5-SNAPSHOT.jar is starting
    ...................................
    exhibition-1.5-SNAPSHOT.jar is start completed. pid is 186794, port is 80
    
  3. 停止

    [root@iccboy app]# ./app.sh stop
    The current app name is:exhibition-1.5-SNAPSHOT.jar
    exhibition-1.5-SNAPSHOT.jar is closing. pid is 186794, port is 80  443
    ..
    exhibition-1.5-SNAPSHOT.jar is shutdown
    
  4. 启动

    [root@iccboy app]# ./app.sh restart
    The current app name is:exhibition-1.5-SNAPSHOT.jar
    exhibition-1.5-SNAPSHOT.jar is starting
    .....................................
    exhibition-1.5-SNAPSHOT.jar is start completed. pid is 188024, port is 80
    
  5. 状态

    [root@iccboy app]# ./app.sh status
    The current app name is:exhibition-1.5-SNAPSHOT.jar
    exhibition-1.5-SNAPSHOT.jar is running. pid is 188024, port is 80  443
    
  6. 命令输入错误

    [root@iccboy app]# ./app.sh xxx
    当前指向程序:exhibition-1.5-SNAPSHOT.jar
    参数输入错误:xxx , 支持以下参数:
    start   :启动程序
    stop    :停止程序
    restart :重启程序
    status  :查看程序运行状态
    

3. 程序进程崩溃自动重启

  1. 创建程序启动脚本,命令为autorun.sh (也存放到/opt/app/目录下)
    #!/bin/sh
    cd /opt/app
    date >> autorun.log
    echo "开始检查" >> autorun.log
    ./app.sh start >> autorun.log
    echo "检查结束" >> autorun.log
    date >> autorun.log
    echo -e "------\n">> autorun.log
    
  2. 加入linux定时任务
    a. 输入命令 crontab -e 编辑定时任务文件,默认是通过 vi编辑器打开该文件
    b. 在文件的最后增加一行配置。如下, 然后保存退出文件。
    */2 * * * * /opt/app/autorun.sh
    
    上面的配置表示每两分钟执行一次。

此时,自动重启生效了。

可以查看自动重启生成的日志文件,tail -f /opt/app/autorun.log

Sun Sep 19 23:18:02 CST 2021
开始检查
The current app name is:exhibition-1.5-SNAPSHOT.jar
exhibition-1.5-SNAPSHOT.jar is already running. pid=176095
检查结束
Sun Sep 19 23:18:02 CST 2021
------

Sun Sep 19 23:20:01 CST 2021
开始检查
The current app name is:exhibition-1.5-SNAPSHOT.jar
exhibition-1.5-SNAPSHOT.jar is starting
..................................
exhibition-1.5-SNAPSHOT.jar is start completed. pid is 180533, port is 80
检查结束
Sun Sep 19 23:20:08 CST 2021
------


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IccBoY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值