使用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