Example 10-6: Monitor the Web site using the while daemon, and determine whether the site is correct every 10 seconds.
#!/bin/bashexport lang= "ZH_CN. UTF-8 "If [$#-ne 1]; Then echo "Usage $ url" exit 1fiwget-o/dev/null-t 5--tries=1--spider #--spider download No files a=$?echo "This URL is ok" if [$a-ne 0] then echo "This is a problematic url:$1" else while true do sleep Wget-o/dev/null-t 5--tries=1 $ echo "This site is normal" Donefi
Another way is to use curl testing:
Curl-o/dev/null--connect-timeout 5-s-W "%{http_code}\n" www.baidu.com: Returns the status code of the Web request-o: Writes the output to a file--connect-timeout: Connect super When 5s-s: Silent state, do not output what and-W combination can get the page request status Code Shell Curl get HTTP Return status code curl-i-M 10-o/dev/null-s-W%{http_code} Www.baidu.com-I only Test HTTP header-M 10 max query 10s-o/dev/null mask original output information-s silent-w%{http_code} control extra output [[email protected] ~]# Curl-o/dev/null--connect -timeout 5-s-W%{http_code} www.baidu.com | Egrep "200|301|302" | Wc-l #如果这个等于1, it is normal to explain this website.
Example: 10-7 use the while daemon to monitor the Web site, every 10s to determine whether the site is normal.
Reference Answer 1: Introducing a library of functions and introducing email alerts in a way that simulates user access
#!/bin/bashsource/etc/init.d/functionswhile Truedo If ["' Curl-o/dev/null--connect-timeout 5-s-W"%{http_code}\n "" $ "| Egrep "200|301|302" | Wc-l ' "-eq 1]; Then action ' This is url:$1 is ok '/bin/true else action ' This is url:$1 is ' bad '/bin/false echo ' this URL: ' Date +%f-%t ' ' | Mail-s "URL test" ***@163.com break fi sleep 10done
Reference Answer 2: Using the shell array method, while detecting multiple URLs is normal, and give professional display effect, this is the actual work of the script.
#!/bin/bashexport lang= "ZH_CN. UTF-8 "Source/etc/init.d/functionsurl= (www.baidu.comwww.jd.commwww.taobao.com) SS () {I=2while ((i>0)) do Echo-n". " Sleep 1 ((i--)) doneprintf ". \ n"}aa () {printf "Test preparation, please later" Ssfor ((a=0;a<3;a++)) do wget-o/dev/null-t 5--tries=1 ${ur L[a]} b=$? If [$b-eq 0] Then action ' this URL ${url[a]} was OK "/bin/true else action" This URL ${url[a]} was bad "/bi N/false echo "This URL is problematic, please deal with it in a timely manner, time fixed in:" ' Date +%f-%t ' "" | Mail-s "URL test" [email protected] fidone}while Truedo AA sleep 10done
Enterprise production Practices: monitoring the Web server is not normal