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

说明:由于截止到目前仅讲了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 /]# echo oldboytest >/var/www/html/index.htm

  [root@oldboy-B /]# wget –quiet –spider http://10.0.0.161/index.htm

  [root@oldboy-B /]# echo $?

  0

  [root@oldboy-B /]# ll index.htm

  ls: index.htm: 没有那个文件或目录
  [root@oldboy-B scripts]# cat apachemon1

  #!/bin/sh

  #created by oldboy 20110523

  

  . /etc/init.d/functions

  #HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l` #=====>这个是基于http方式进行判断

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

  exit 1

  else

  action “httpd is running” /bin/true

  exit 0

  fi
真正使用时,有些输出是不需要的就去掉
  [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" \< "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

案例五:shell脚本实现定时监控http服务的运行状态的更多相关文章

  1. linux shell脚本守护进程监控svn服务

    最近搭建的svn服务不知道什么原因服务总是被关闭(如果你不知道怎么搭建svn可以参考linux下搭建svn版本控制软件),因此用shell脚本实现一个守护进程.用于监控svn服务是否启动,如果服务不在 ...

  2. 【shell脚本】自动监控tomcat服务===autoCheck.sh

    自动监控tomcat服务,当tommcat服务挂掉时自动重启 一.脚本内容 [root@localhost ]# cat /root/autoCheck.sh #!/bin/bash startTom ...

  3. CentOS/Linux内存占用大,用Shell脚本自动定时清除/释放内存

    CentOS/Linux内存占用大,用Shell脚本自动定时清除/释放内存来自:互联网 时间:2020-03-22 阅读:114以下情况可能造成Linux内存占用过高服务配置存在直接分配错误,或隐性分 ...

  4. Dubbo入门到精通学习笔记(二):Dubbo管理控制台、使用Maven构建Dubbo的jar包、在Linux上部署Dubbo privider服务(shell脚本)、部署consumer服务

    文章目录 Dubbo管理控制台 1.Dubbo管理控制台的主要作用: 2.管理控制台主要包含: 3.管理控制台版本: 安装 Dubbo 管理控制台 使用Maven构建Dubbo服务的可执行jar包 D ...

  5. Git学习-->如何通过Shell脚本自动定时将Gitlab备份文件复制到远程服务器?

    一.背景 在我之前的博客 git学习--> Gitlab如何进行备份恢复与迁移? (地址:http://blog.csdn.net/ouyang_peng/article/details/770 ...

  6. shell脚本实例-系统监控

    shell脚本监控网站并实现邮件.短信报警shell进程监控脚本(发送邮件报警)Shell脚本监控服务器在线状态和邮件报警的方法 http://www.jbxue.com/jb/shell/ 11. ...

  7. 用shell脚本实现定时备份数据库

    1.备份数据库的方法 可以使用命令查看 ls  /usr/local/mysql/bin 这个mysqldump就是系统内置的用来备份数据库的工具. 2.实现方法 ①先随便进入一个位置创建一个目录 ② ...

  8. Shell脚本日志关键字监控+告警

    最近小张的爬虫程序越来越多,可当爬虫程序报错,不能及时的发现,从而造成某些重要信息不能及时获取的问题,更有甚者,遭到领导的批评.于是就在想有没有一种方法,当爬取信息报错的时候,可以通过邮件或者短信的方 ...

  9. 实验五 shell脚本编程

    项目 内容 这个作业属于哪个课程 课程链接 这个作业的要求在哪里 作业要求 学号-姓名 17041428-朱槐健 作业学习目标 1. 了解shell脚本的概念及使用 2.掌握shell脚本语言的基本语 ...

随机推荐

  1. vscode配置Microsoft C++注意事项

    vscode配置c++插件教程链接如下: https://code.visualstudio.com/docs/cpp/config-msvc#_check-your-microsoft-visual ...

  2. python 使用sqlite,ConfigParser实例

    此实例是本人公司真实场景,使用了VNC,ngrok 技术实现内网穿透,本例是对内网穿透的使用: 此例的最终效果是对于处于各地内网终端实现远程桌面监控及操作: 目前世面上也有一些软件实现了内网穿透(向日 ...

  3. Kubernetes的故事之持久化存储(十)

    一.Storage 1.1.Volume 官网网址:https://kubernetes.io/docs/concepts/storage/volumes/ 通过官网说明大致总结下就是这个volume ...

  4. 直接copy大于某一个时间小于某一个时间的文件--find进阶用法

    find ./ -type f -newermt '2000-01-04 10:30:00' ! -newermt '2019-10-28 10:57:00' -exec cp -a {} /var/ ...

  5. 用socket写一个简单的服务器

    import socketsk=socket.socket()sk.bind(("127.0.0.1",7001))sk.listen()def login(url): with ...

  6. ABC209 E Shiritori

    考虑对这个问题进行转化: 显然我们只关注每个串前三个棋子和后三个棋子,并且根据题目的特性,我们可以将任意的三个字符看作点,将一个字符串看作连接两个点的边,这样我们得到了一张点数为 \(52 ^ 3\) ...

  7. AT2650 [ARC077C] guruguru

    可以发现,如果我们枚举每个理想亮度 \(X\) 然后再求在这个理想亮度情况下的答案是非常难维护的. 不妨反过来,考虑每个位置 \(i, i + 1\) 之间对每个理想亮度 \(X\) 减少次数的贡献. ...

  8. Spring service本类中方法互相调用事物失效问题

    简介 Spring事物利用的是AOP,动态代理采用CGLIB代理(默认,也可以用Proxy代理,但是Proxy代理效率低于CGLIB代理).故只要弄懂Spring的AOP实现,就知道为什么servic ...

  9. 原生js获取子元素

    感谢原文作者:归一山人 原文链接:https://www.cnblogs.com/guiyishanren/p/12214757.html 获取子元素的方法有 //获取第一个demo类 dom = d ...

  10. K8是—— yaml资源清单

    K8是-- yaml资源清单 一.yaml文件详解 1.Kubernetes支持YAML和JSON格式管理资源对象2.JSON格式:主要用于api接口之间消息的传递3.YAML格式:用于配置和管理,Y ...