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