1,監控httpd狀態碼的shell腳本代碼。
#!/bin/sh #site: www.jquerycn.cn # website[0]=www.jquerycn.cn/chuzu/' #網站1 mobile[0]='13141200000' #對應網站1 手機號碼 website[1]=www.jquerycn.cn/chushou/' #同上2 mobile[1]='13141200000' #同上2 #當網站較多時,可以考慮以文件來存儲,或從數據庫中讀取 length=${#website[@]} #獲取網站總數量 for ((i=0; i<$length; i++)) #循環執行 do status=$(curl -I -m 10 -o /dev/null -s -w %{http_code} ${website[$i]}) #CURL 獲取http狀態碼 if [ "$status"x != "200"x ]; then #檢測是否為 200(正常) echo ${website[$i]} '=>' $status #php /htdoc/jk/shell_monitor.php ${mobile[$i]} ${website[$i]}'=>AccessError!' #執行PHP文件(采用第三方短信類庫,或發送報警郵件) fi #結束if done #結束 do
執行:crontab -e
寫入內容:5 * * * * /shell path
(上5分鍾執行一次)
2,監控網站狀態的shell腳本
監控指定網頁的狀態,若返回200表示正常,否則出錯:
#!/bin/bash #edit: www.jquerycn.cn #網站url地址 # URL=$1 #獲取http響應代碼 HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"` #服務器能正常響應,應該返回200的代碼 if [ $HTTP_CODE != 200 ];then echo $HTTP_CODE ((a=$HTTP_CODE/10)) echo $a #這里可以報警處理 exit $a else exit 0 fi
調用腳本的方法:
bash ./url_monitor.sh "http://www.jquerycn.cn/"
設置crontab計划任務,每五分鍾執行一次,以監控網站的健康狀態。
示例:
5 * * * * ./url_monitor.sh