今天給自己的網站寫了shell監控,避免網站掛了我都不知道,這個指令碼是分別檢查網頁狀態和網頁連線時間,還可以根據自己的需要進行擴充,很強大.
指令碼內容:
vi check-web.sh
代碼如下 |
複製代碼 |
#!/bin/sh weblist=/root/weblist.txt for list in `cat $weblist|grep -E -v "#|^$"` do httpcode=`curl -o /dev/null -s -w %{http_code} "$list"` httptime=`curl -o /dev/null -s -w "time_connect: %{time_connect}ntime_starttransfer:%{time_starttransfer}ntime_total: %{time_total}n" "$list"|grep time_total|awk -F ":" '{print $2*1000}'` #if [ $httpcode = 200 ]||[ $httpcode = 301 ]||[ $httpcode = 302 ]||[ $httpcode = 403 ]||[ $httpcode = 401 ] if [ $httpcode = 200 ]||[ $httpcode = 301 ]||[ $httpcode = 302 ] then echo "$list is checked ok!" else echo "$list is down!" | mutt -s "web is down" rocdk@163.com fi if [ $httptime -ge 10000 ] then echo "$list is timeout!" | mutt -s "web is timeout" rocdk@163.com else echo "$list is connect ok!" fi done |
然後建立要檢查網站的列表,格式為http://xxx.xxx.xxx
代碼如下 |
複製代碼 |
touch /root/weblist.txt http://blog.slogra.com
chmod +x /root/soft_shell/check-web.sh
crontab -e */3 * * * * /bin/sh /root/soft_shell/check-web.sh |
手動運行後的圖如下
好了,這下可以放心了.