SpringBoot在Linux上优雅重启

Spring Boot项目中加入

import lombok.extern.slf4j.Slf4j;

import javax.annotation.PreDestroy;

@Slf4j
public class TerminateBean {

	@PreDestroy
	public void preDestroy() {
		log.info("程序正常退出");
        //在这里做程序关闭之前的事情
	}

}
@Bean
public TerminateBean TerminateBean() {
    //实例化该对象
    return new TerminateBean();
}

Linux重启脚本如下:

#!/bin/sh
APP_DIR=/mnt/app/spring
APP_NAME=spring-biz.jar
MAX_TIMEOUT=20
echo : [app dir] $APP_DIR
echo : [app name] $APP_NAME

pid=`ps -ef | grep java |grep $APP_NAME | grep -v grep |awk '{print $2}'`

if [ -n "$pid" ]; then
  echo : $APP_NAME is running pid=$pid
  echo : kill -15 $APP_NAME
  kill -15 $pid
fi

for((i=0;i<$MAX_TIMEOUT;i++))
do
  sleep 1
  pid=`ps -ef | grep java |grep $APP_NAME | grep -v grep |awk '{print $2}'`
  if [ -n "$pid" ]; then
    echo : Stoping...
  else
    break
  fi
done


if [ -n "$pid" ]; then
  echo : $APP_NAME is running pid=$pid
  echo : kill -9 $APP_NAME
  kill -9 $pid
fi

pid=`ps -ef | grep java |grep $APP_NAME | grep -v grep |awk '{print $2}'`
if [ -n "$pid" ]; then
  echo : Running
else
  echo : Stopped
fi

cd $APP_DIR
nohup java -Dfile.encoding=utf-8 -jar -Xms512m -Xmx512m $APP_NAME > $APP_DIR/nohup.out 2>&1 &
echo : start $APP_NAME

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值