標籤:shell指令碼檢查網站狀態
檢查網站狀態通常使用wget或curl工具,下面分別使用這二種工具來做寫檢查網站的指令碼。(學習自老男孩shell編程)
命令列:
1、curl得到傳回值200,表示正常
|
[[email protected] ~]# curl -o /dev/null -s --connect-timeout 5 -w ‘%{http_code}‘ www.baidu.com
200[[email protected] ~] |
2、wget得到0,表示正常
| [[email protected] ~]# wget -T 5 -t 2 --spider www.baidu.com &>/dev/null
[[email protected] ~]# echo $?
0 |
指令碼:
1、culr
|
[[email protected] shell]# cat check_web1.sh
#!/bin/bash
#
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
array=(
http://www.china-cmd.org
http://www.cmdmedia.cn
http://www.icehtmc.com
https://mail.cmdmedia.cn
)
curl_ip() {
CURL=$(curl -o /dev/null -s --connect-timeout 5 -w ‘%{http_code}‘ $1|egrep "200|302"|wc -l)
return $CURL
}
main() {
for n in ${array[*]}
do
curl_ip $n
if [ $? -eq 1 ];then
action "curl $n" /bin/true
else
action "curl $n" /bin/false
sleep 3
CURL=$(curl_ip $n|egrep "200|302"|wc -l)
if [ $CURL -eq 1 ];then
action "Retry curl $n again" /bin/true
else
action "Retry curl $n again" /bin/false
fi
fi
done
}
main |
圖示:
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/8D/BB/wKiom1ioYOWCw9-QAAFDKf159i8710.jpg" title="QQ20170218225738.jpg" alt="wKiom1ioYOWCw9-QAAFDKf159i8710.jpg" />
2、wget
|
[[email protected] shell]# cat check_web2.sh
#!/bin/bash
#
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
array=(
http://www.china-cmd.org
http://www.cmdmedia.cn
http://www.icehtmc.com
https://mail.cmdmedia.cn
)
curl_ip() {
wget -T 5 -t 2 --spider $1 &>/dev/null
return $?
}
main() {
for n in ${array[*]}
do
curl_ip $n
if [ $? -eq 0 ];then
action "curl $n" /bin/true
else
action "curl $n" /bin/false
fi
done
}
main |
圖示:
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/8D/B9/wKioL1ioYWHxSX3dAAFI8IzODi8221.jpg" title="QQ20170218225959.jpg" alt="wKioL1ioYWHxSX3dAAFI8IzODi8221.jpg" />
學習自:
老男孩shell
本文出自 “趙東偉的部落格” 部落格,請務必保留此出處http://zhaodongwei.blog.51cto.com/4233742/1899145
shell指令碼檢查網站狀態