欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

监控网站是否可以正常打开的Shell脚本分享

程序员文章站 2023-11-18 15:34:46
最近刚好需要测试一下新建站的稳定性,所以写了个shell脚本放到本机(最近换了mac本),能够实时查看你需要监控的web页面状态,并发送到指定邮箱. 这里赞一下os x自...

最近刚好需要测试一下新建站的稳定性,所以写了个shell脚本放到本机(最近换了mac本),能够实时查看你需要监控的web页面状态,并发送到指定邮箱.

这里赞一下os x自带有crontab计划任务,可以直接在本机测试脚本啦^_^

# vi check_web_alive.sh

复制代码 代码如下:

#!/bin/bash
path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export path
# define url
web_url=("http://www.example.com" "http://www1.example.com" "http://www2.example.com")

# check network
net_alive=$(ping -c 5 8.8.8.8 |grep 'received'|awk 'begin {fs=","} {print $2}'|awk '{print $1}')
if [ $net_alive == 0 ]; then
    echo "network is not active,please check your network configuration!"
    exit 0
fi
# check url
for((i=0; i!=${#web_url[@]}; ++i))
{
  alive=$(curl -o /dev/null -s -m 10 -connect-timeout 10 -w %{http_code} ${web_url[i]} |grep"000000")
  if [ "$alive" == "000000" ]; then
    echo "'${web_url[i]}' can not be open,please check!" | mail -s "website notification to ${web_url[i]}" yourname@example.com
    echo "failed"
  else
    echo "'${web_url[i]}' is ok!"
  fi
}