【springboot】之利用shell脚本优雅启动,关闭springboot服务
本文内容纲要:
springbot开发api接口服务,生产环境中一般都是运行独立的jar,在部署过程中涉及到服务的优雅启动,关闭,
springboot官方文档给出的有两种方式,
1、使用httpshutdown
2、注册为系统服务https://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#deployment-service
第一种不方便,麻烦,需要配置各种安全策略,无法集成自动化部署工具
第二种需要建立软连接,可控性不够
尤其是集成自动化部署工具之后,使用shell脚本是不错的选择
#!/bin/sh
##javaenv
exportJAVA_HOME=/usr/local/jdk/jdk1.8.0_101
exportJRE_HOME=$JAVA_HOME/jre
API_NAME=api
JAR_NAME=$API_NAME\.jar
#PID代表是PID文件
PID=$API_NAME\.pid
#使用说明,用来提示输入参数
usage(){
echo"Usage:sh执行脚本.sh[start|stop|restart|status]"
exit1
}
#检查程序是否在运行
is_exist(){
pid=`ps-ef|grep$JAR_NAME|grep-vgrep|awk'{print$2}'`
#如果不存在返回1,存在返回0
if[-z"${pid}"];then
return1
else
return0
fi
}
#启动方法
start(){
is_exist
if[$?-eq"0"];then
echo">>>${JAR_NAME}isalreadyrunningPID=${pid}<<<"
else
nohup$JRE_HOME/bin/java-Xms256m-Xmx512m-jar$JAR_NAME>/dev/null2>&1&
echo$!>$PID
echo">>>start$JAR_NAMEsuccessedPID=$!<<<"
fi
}
#停止方法
stop(){
#is_exist
pidf=$(cat$PID)
#echo"$pidf"
echo">>>apiPID=$pidfbeginkill$pidf<<<"
kill$pidf
rm-rf$PID
sleep2
is_exist
if[$?-eq"0"];then
echo">>>api2PID=$pidbeginkill-9$pid<<<"
kill-9$pid
sleep2
echo">>>$JAR_NAMEprocessstopped<<<"
else
echo">>>${JAR_NAME}isnotrunning<<<"
fi
}
#输出运行状态
status(){
is_exist
if[$?-eq"0"];then
echo">>>${JAR_NAME}isrunningPIDis${pid}<<<"
else
echo">>>${JAR_NAME}isnotrunning<<<"
fi
}
#重启
restart(){
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case"$1"in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
exit0
本文内容总结:
原文链接:https://www.cnblogs.com/gyjx2016/p/8462929.html