项目中需要监控java后台服务是否运行,如果后台停止运行,则重启。具体步骤如下
1、编写shell脚本check.sh
#!/bin/bash
. ~/.bash_profile
===========================================
#核查日志文件名中关于时间格式
LOG_DATEFMT=$(date +%Y%m%d%H)
USERNAME=wlsr
#重启时间
chk_time=`date +%Y%m%d%H%M%S`
===========================================
function checkProcess() {
echo "---------------------------------------------------------------------------------------"
echo " $chk_time $3程序核查 "
echo "---------------------------------------------------------------------------------------"
if [ $# -ne 3 ]
then
echo "Error: checkProcess() 参数非法"
else
ps -f |grep -w dubbo-service | grep -v grep | wc -l
processCount=$(ps -f -u $USERNAME |grep -w "$1" | grep -v grep | wc -l)
if [ $processCount -eq 0 ]
then
echo "Error:$1 进程不存在或进程数错误,进程数 $processCount"
cd $2
$3
echo "Error: $chk_time: restart $1"
else
echo "Info:$chk_time: $1 进程存在,进程数 $processCount "
fi
fi
}
checkProcess "dubbo-service" "/home/wxbsusht/dubbo-service" "./run.sh start"
checkProcess "dubbo-consume" "/home/wxbsusht/dubbo-consume" "./run.sh start"
exit 0
2、将shell脚本添加到crontab中
crontab -e 加入如下内容
1-59 * * * * /home/wxbsusht/check-pid-shell/check.sh >/dev/null