脚本检测url是否正常

使用shell脚本检测url是否正常

1.while 循环

#!/bin/bash
if [ $# -ne 1 ]
  then
    echo $"usage $0 url"
    exit 1
fi

while true
  do
    if [ `curl -o /dev/null --connect-timeout 5 -s -w "%{http_code}" $1|egrep -w "200|301|302"|wc -l` -ne 1 ]
      then 
        echo "$1 is error."
      else
        echo "$1 is ok"
    fi 
    sleep 10
done
#!/bin/bash
. /etc/init.d/functions
if [ $# -ne 1 ]
  then
    echo $"usage $0 url"
    exit 1
fi

while true
  do 
  if [ `curl -o /dev/null --connect-timeout 5 -s -w "%{http_code}" $1|egrep -w "200|301|302"|wc -l` -ne 1 ]
    then
      action "$1 is error." /bin/false
    else
      action "$1 is ok." /bin/true
  fi
  sleep 10
done

函数方式

#!/bin/bash
. /etc/init.d/functions
check_count=0
url_list=(
http://www.langwenke.com
http://testm.langwenke.com
http://db.one1tree.com.cn
)

function wait () {
echo -n '3秒后,执行检查url操作.';
for ((i=0;i<3;i++))
  do
    echo -n "."
    sleep 1
  done
  echo 
}

function check_url () {
  wait
    for ((i=0;i<`echo ${#url_list[*]}`;i++))
      do
        wget -o /dev/null -T 3 --tries=1 --spider ${url_list[$i]} >/dev/null 2>&1
        if [ $? -eq 0 ]
          then
            action "${url_list[$i]}" /bin/true
          else
            action "${url_list[$i]}" /bin/false
        fi
      done
      ((check_count++))
}

main () {
  while true
    do
      check_url
      echo "-----------check count:${check_count}-----------------"
      sleep 10
    done
}

main

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值