新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Shell腳本習(xí)題:定時(shí)監(jiān)控http服務(wù)的運(yùn)行狀態(tài)
注意:監(jiān)控方法可以為端口、進(jìn)程、URL模擬訪(fǎng)問(wèn)方式,或者三種方法綜合。

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為數(shù)千家客戶(hù)提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為寧都企業(yè)提供專(zhuān)業(yè)的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,寧都網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
說(shuō)明:由于截止到目前僅講了if語(yǔ)句,因此,就請(qǐng)大家用if語(yǔ)句來(lái)實(shí)現(xiàn)。
[root@oldboy-B scripts]# cat apachemon
#!/bin/sh
#created by oldboy 20110523
. /etc/init.d/functions
HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`
#if [ $HTTPPRONUM -lt 1 ];then
if [[ $HTTPPRONUM -lt 1 ]];then
action “httpd is not running” /bin/false
action “httpd is not running” /bin/false >/tmp/httpd.log
httpdctl restart >/dev/null 2>&1
action “httpd is restart” /bin/true
mail -s “`uname -n`’s httpd restarted at `(date)`” [email protected]
exit 1
else
action “httpd is running” /bin/true
exit 0
fi
[root@oldboy-B scripts]# apachemon
httpd is running [確定]
[root@oldboy-B scripts]# pkill httpd
[root@oldboy-B scripts]# ps -ef |grep http |grep -v grep
[root@oldboy-B scripts]# apachemon
httpd is not running [失敗]
httpd is restart [確定]
[root@oldboy-B scripts]# ps -ef|grep http|grep -v grep
root 5845 1 1 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5852 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5853 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5854 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5855 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5856 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5857 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5858 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
apache 5859 5845 0 15:59 ? 00:00:00 /usr/sbin/httpd -k restart
腳本改進(jìn)
真正使用時(shí),有些輸出是不需要的就去掉
[root@oldboy-B scripts]# cat apachemon1
#!/bin/sh
#created by oldboy 20110523
#
. /etc/init.d/functions
wget –quiet –spider http://10.0.0.161/index.htm #=====>這個(gè)是基于WGET URL方式進(jìn)行判斷
if [ $? -ne 0 ];then
action “httpd is not running” /bin/false >/tmp/httpd.log
httpdctl restart >/dev/null 2>&1
action “httpd is restart” /bin/true >>/tmp/httpd.log
mail -s “`uname -n`’s httpd restarted at `(date)`” [email protected]
exit 1
fi
多條件判斷的寫(xiě)法
[root@oldboy-B scripts]# cat apachemon1
#!/bin/sh
#created by oldboy 20110523
#
. /etc/init.d/functions
HTTPPORTNUM=`netstat -lnt|grep 80|grep -v grep|wc -l`
HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`
wget –quiet –spider http://10.0.0.161/index.htm && RETVAL=$?
if [ $RETVAL -ne 0 ] || [ $HTTPPORTNUM -ne 1 ] || [ $HTTPPRONUM -lt 1 ] ;then
#if [ "$RETVAL" != "0" -o "$HTTPPORTNUM" != "1" -o "$HTTPPRONUM" \ action “httpd is not running” /bin/false action “httpd is not running” /bin/false >/tmp/httpd.log httpdctl restart >/dev/null 2>&1 action “httpd is restart” /bin/true mail -s “`uname -n`’s httpd restarted at `(date)`” [email protected] exit 1 else action “httpd is running” /bin/true exit 0 fi 當(dāng)前文章:Shell腳本習(xí)題:定時(shí)監(jiān)控http服務(wù)的運(yùn)行狀態(tài)
網(wǎng)站鏈接:http://m.fisionsoft.com.cn/article/dpcipci.html


咨詢(xún)
建站咨詢(xún)
