#!/bin/bash
##
#Program:check502
##
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# web_URL #
site=测试网页地址
# mailto #
receiver_email="接受邮箱"
sender_email="发送邮箱"
recevier="Admin"
sender="Zeroto"
# email #
function run_502(){
sendmail -t <<EOF
From: $sender <$sender_email>
To: $receiver <$receiver_email>
Subject: VPS has encountered a 502 error!
----------------------------------------
The VPS has encountered a 502 error, and it has been handled.
Congratulations! The VPS runs well now!
----------------------------------------
EOF
}
function restart_error(){
sendmail -t <<EOF
From: $sender <$sender_email>
To: $receiver <$receiver_email>
Subject: A terrible error occurred to the VPS.
----------------------------------------
The VPS has encountered an error, but it failed to restart php-fpm.
The information has already saved to /home/wwwlogs/502log.log.
----------------------------------------
EOF
}
# test #
stat=`curl -I $site | head -1 | cut -d ' ' -f2`
if [ $stat == "502" ]; then
service php-fpm restart 2>>/home/wwwlogs/502log.log
temp=`echo $?`
if [ $temp != "0" ]; then
restart_error
else
run_502
fi
fi
exit 0
1测试网页必须在同一个服务器上,可以自己创建一个php:
<?php echo "This is a test page!"; ?>
脚本保存成 /home/check502.sh ,并授权 chmod 755 。
添加定时任务,每十分钟检查一次状态并写记录。crontab –l 检查任务添加。
*/10 * * * * (/home/check502.sh >> /home/wwwlogs/502log.log)
