Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
监控页面,如果访问出错,重启docker
#!/bin/sh
# 注意权限,如果docker是root用户运行,运行此个脚本也需要root用户运行
# 自己定义要监控的页面地址,页面越简单越好,比如:页面上写个success即可
# 这个链接是wordpress安装页面,如已安装,将显示提示页面,页面内容少,仅1.1k大小
WebUrl=https://lidong.me/wordpress/wp-admin/install.php
# 日志输出 (自己定义地址,用于输出监控日志和监控报错日志)
MonitorLog=/var/log/monitor/Monitor.log
# 根据日期设置下载保存目录
SavedPagePath=/var/log/monitor/$(date +'%F')
# 下载页面保存的完整名称
SavedPageName=${SavedPagePath}"/"$(date +'%H%M%S')".html"
# 如果目录不存在,创建目录
if [ ! -d "${SavedPagePath}" ]; then
mkdir ${SavedPagePath}
fi
Monitor(){
echo "[info][$(date +'%F %H:%M:%S')]开始监控网页..."
# 检测是否启动成功(成功的话页面会返回状态"200")
ServiceCode=$(curl -s -o $SavedPageName -m 10 --connect-timeout 10 $WebUrl -w %{http_code})
if [ $ServiceCode -eq 200 ];then
echo "[info][$(date +'%F %H:%M:%S')]页面返回码为$ServiceCode,测试页面正常......"
else
echo "[error][$(date +'%F %H:%M:%S')] 页面返回状态码为$ServiceCode,错误日志已输出到$SavedPageName,请注意......"
echo "[error][$(date +'%F %H:%M:%S')] 页面访问出错,开始重启docker"
systemctl stop docker && systemctl start docker # 重启docker
sleep 5
fi
echo "------------------------------"
}
Monitor>>$MonitorLog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment