天天看點

shell腳本檢查網站是否正常

檢查Web網站可用性的常見腳本

  1. 法一
#!/bin/bash
...
function usage(){
echo $"usage:$0
exit 1
}

function check_url() {

wget --spider -q -o /dev/null --tries=1 -T 5 $1
if [ $? -eq 0 ]
then
echo "$1
exit 0
else
echo "$1
exit 1
fi
}
...      
  1. 法二
#!/bin/bash
...
. /etc/init.d/functions
num=`curl -I -m 5 -s -w "%{http_code}\n" -o /dev/null 192.168.100.141:8080`
if [ $num -eq 200 ]
then action "ok!" /bin/true
else action "failure" /bin/false
fi
...      
  1. 法三(推薦)

    主要測試指令

# 腳本判斷
curl -s -o /dev/null www.url.com
# 人判斷
curl      
#!/bin/bash
...
. /etc/init.d/functions
curl -s -o /dev/null 192.168.100.141:8080
if [ $? -eq 0 ]
then action "Web site access is normal" /bin/true
else action "Failure of website access" /bin/false
fi
...      
  1. 法四
#!/bin/bash
...
. /etc/init.d/functions
wget --spider -T 5 -q -t 2 192.168.100.141:8080

if [ $? -eq 0 ]
then action "Web site access is normal" /bin/true
else action "Failure of website access" /bin/false
fi
...      

繼續閱讀