java应用健康检查

时间:2023-03-09 00:36:52
java应用健康检查

本文主要针对自己手写shell监控应用状态,有可系统解决方案的,比如K8S,可以略过

#!/bin/sh
#health_check.sh
count=`ps -ef | grep test.jar | grep -v grep|wc -l`
http_code=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://127.0.0.1:8080/health/check`
if [ ${count} -eq 0 ] ; then
nohup java -jar /data/tomcat/test.jar >/dev/null 2>&1
elif [ ${http_code} -ne 200 ]; then
/data/tomcat/restart.sh
else
echo "test process is running"
fi

restart.sh脚本内容:

#!/bin/sh
#restart.sh ps -ef |grep test.jar |awk '{print $2}'|xargs kill -9 sleep 3 nohup java -jar /data/tomcat/test.jar >/dev/null 2>&1 &
note:
在编写shell脚本时,遇到一下小坑,记录如下
1.使用notepad++编写shell时,注意把文件的格式设置成UNIX,否则linux会不识某些字符,比如换行,以免造成不必要的困惑.
2.本文以启动spring boot可执行jar包为例,使用nohup时,结尾要加上 >/dev/null 2>&1 &
不然,启动日志会输出到jinkens里,显示不稳定(UNSTABLE)状态
3.if/elif/else[]关键字符前后在留有空格