#!/bin/bash
service_name=xxx.jar
pid=`ps -ef|grep $service_name|grep -v 'grep'|awk '{print $2}'`
c_time=`date +"%Y-%m-%d %H:%M:%S"`
port=8080
# 安装目录
install_dir=/home/service/xxx
# 诊断接口
diagnose_url=/actuator/health
if [ $pid ]
then
code=`curl --max-time 2 -o ./health.check -s -w %{http_code} "http://127.0.0.1:$port/$diagnose_url"`
if [ "$code"x = "200"x ]
then
echo "$c_time 服务[$service_name]正常,PID:$pid"
else
kill_process=true
for i in `seq 3`
do
t_time=`date +"%Y-%m-%d %H:%M:%S"`
echo "$t_time 第$i次重试..."
sleep 5
code=`curl --max-time 2 -o ./health.check -s -w %{http_code} "http://127.0.0.1:$port/$diagnose_url"`
if [ "$code"x = "200"x ]
then
kill_process=false
break
fi
done
if [ "$kill_process" = true ]
then
r_time=`date +"%Y-%m-%d %H:%M:%S"`
echo "$r_time 服务[$service_name]假死,重启中..."
jmap -dump:format=b,file=$pid.hprof $pid
kill -9 $pid
source /etc/profile
# nohup java -Xms1024m -Xmx1024m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$install_dir/dump.hprof -XX:OnOutOfMemoryError="kill -9 %p" -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCCause -Xloggc:$install_dir/gc_logs/gc_%t.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=8 -XX:GCLogFileSize=10M -jar $install_dir/$service_name -Duser.timezone=GMT+08 --spring.profiles.active=prod -Djava.security.egd=file:/dev/./urandom &
sh $install_dir/deploy.sh restart prod
fi
fi
else
echo "$c_time 服务[$service_name]宕机,重启中..."
source /etc/profile
#jmap -dump:format=b,file=$pid.hprof $pid
#nohup java -Xms1024m -Xmx1024m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$install_dir/dump.hprof -XX:OnOutOfMemoryError="kill -9 %p" -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCCause -Xloggc:$install_dir/gc_logs/gc_%t.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=8 -XX:GCLogFileSize=10M -jar $install_dir/$service_name -Duser.timezone=GMT+08 --spring.profiles.active=prod -Djava.security.egd=file:/dev/./urandom &
sh $install_dir/deploy.sh restart prod
fi
配合Crontab或Supervisor使用。