这篇文章主要介绍“Shell如何定时监控http服务的运行状态”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Shell如何定时监控http服务的运行状态”文章能帮助大家解决问题。

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

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

[root@oldboy-Bscripts]#catapachemon#!/bin/sh#createdbyoldboy20110523./etc/init.d/functionsHTTPPRONUM=`ps-ef|grephttp|grep-vgrep|wc-l`#if[$HTTPPRONUM-lt1];thenif[[$HTTPPRONUM-lt1]];thenaction“httpdisnotrunning”/bin/falseaction“httpdisnotrunning”/bin/false>/tmp/httpd.loghttpdctlrestart>/dev/null2>&1action“httpdisrestart”/bin/truemail-s“`uname-n`’shttpdrestartedat`(date)`”31333741@qq.comexit1elseaction“httpdisrunning”/bin/trueexit0fi[root@oldboy-Bscripts]#apachemonhttpdisrunning[确定][root@oldboy-Bscripts]#pkillhttpd[root@oldboy-Bscripts]#ps-ef|grephttp|grep-vgrep[root@oldboy-Bscripts]#apachemonhttpdisnotrunning[失败]httpdisrestart[确定][root@oldboy-Bscripts]#ps-ef|grephttp|grep-vgreproot58451115:59?00:00:00/usr/sbin/httpd-krestartapache58525845015:59?00:00:00/usr/sbin/httpd-krestartapache58535845015:59?00:00:00/usr/sbin/httpd-krestartapache58545845015:59?00:00:00/usr/sbin/httpd-krestartapache58555845015:59?00:00:00/usr/sbin/httpd-krestartapache58565845015:59?00:00:00/usr/sbin/httpd-krestartapache58575845015:59?00:00:00/usr/sbin/httpd-krestartapache58585845015:59?00:00:00/usr/sbin/httpd-krestartapache58595845015:59?00:00:00/usr/sbin/httpd-krestart脚本改进真正使用时,有些输出是不需要的就去掉

[root@oldboy-Bscripts]#catapachemon1#!/bin/sh#createdbyoldboy20110523#./etc/init.d/functionswget–quiet–spiderhttp://10.0.0.161/index.htm#=====>这个是基于WGETURL方式进行判断if[$?-ne0];thenaction“httpdisnotrunning”/bin/false>/tmp/httpd.loghttpdctlrestart>/dev/null2>&1action“httpdisrestart”/bin/true>>/tmp/httpd.logmail-s“`uname-n`’shttpdrestartedat`(date)`”31333741@qq.comexit1fi多条件判断的写法

[root@oldboy-Bscripts]#catapachemon1#!/bin/sh#createdbyoldboy20110523#./etc/init.d/functionsHTTPPORTNUM=`netstat-lnt|grep80|grep-vgrep|wc-l`HTTPPRONUM=`ps-ef|grephttp|grep-vgrep|wc-l`wget–quiet–spiderhttp://10.0.0.161/index.htm&&RETVAL=$?if[$RETVAL-ne0]||[$HTTPPORTNUM-ne1]||[$HTTPPRONUM-lt1];then#if["$RETVAL"!="0"-o"$HTTPPORTNUM"!="1"-o"$HTTPPRONUM"\action“httpdisnotrunning”/bin/falseaction“httpdisnotrunning”/bin/false>/tmp/httpd.loghttpdctlrestart>/dev/null2>&1action“httpdisrestart”/bin/truemail-s“`uname-n`’shttpdrestartedat`(date)`”31333741@qq.comexit1elseaction“httpdisrunning”/bin/trueexit0fi

关于“Shell如何定时监控http服务的运行状态”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。