Shell脚本习题:定时监控http服务的运行状态

注意:监控方法可以为端口、进程、URL模拟访问方式,或者三种方法综合。

成都创新互联公司长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为宁都企业提供专业的网站设计、成都网站制作宁都网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。

说明:由于截止到目前仅讲了if语句,因此,就请大家用if语句来实现。

  [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)`” 31333741@qq.com

  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

脚本改进

 

真正使用时,有些输出是不需要的就去掉

  [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 #=====>这个是基于WGET URL方式进行判断

  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)`” 31333741@qq.com

  exit 1

  fi

多条件判断的写法

  [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)`” 31333741@qq.com   exit 1   else   action “httpd is running” /bin/true   exit 0   fi 

网站名称:Shell脚本习题:定时监控http服务的运行状态
链接URL:http://www.hantingmc.com/qtweb/news24/132074.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联