设为首页 加入收藏

TOP

开发shell脚本实现定时(每隔5分钟)监控http服务的运行状态,如果未运行则自动
2014-11-06 08:30:05 来源: 作者: 【 】 浏览:37
Tags:开发 shell 脚本 实现 定时 每隔 5分钟 监控 http 服务 运行 状态 如果 自动

注意:监控方法可以为端口、进程、、URL模拟访问方式,或者三种方法综合。
说明:由于截止到目前仅讲了if语句,因此,就请大家用if语句来实现。
考试时间:15分钟


[root@oldboy-B scripts]# cat apachemon
#!/bin/sh
#created by oldboy 20110523
#QQ 31333741
. /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
#QQ 31333741
. /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
#QQ 31333741
. /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
#QQ 31333741
. /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


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Oracle基本操作笔试面试题之用户.. 下一篇java应聘教师测试试题一及答案

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: