码迷,mamicode.com
首页 > 系统相关 > 详细

linux运维学习shell脚本监控nginx服务

时间:2016-02-26 12:30:31      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:监控nginx   linux运维   shell脚本学习   

        菜鸟学习shell脚本后,动手练手简单小实验,使用shell脚本判断nginx是否正常运行,如果没有运行将nginx服务启动起来。

一、基于进程判断

1、获取nginx进程来判断该服务是否正常启动。

        ps -ef | grep nginx | grep -v grep | wc -l    输出进程行数 然后判断是否为2 如果不是说明该服务没有启动。

         grep -v grep  用来排除掉您所使用grep产生的进程。

2、shell 脚本编写

     先定义一个变量Web

            #!/bin/bash

            Web=`ps -ef |grep nginx|grep -v grep|wc -l`

            if [ $Web -eq 2 ];then

                     echo "you nginx start"

                     exit 0

                else

                     service nginx start

                     exit 1

            fi

定义一个变量Web ,然后判断如果不成立,将使用service nginx start启动服务。

 二、基于端口的判断

    虽然都是获取的数字,但是由于端口不存在的话为空,所以判断是用的是字符串的判断

也就是把判断的运算符使用"="而不是"-eq"

            #!/bin/bash

            WebPort=`netstat -nlt |grep 80|awk ‘{print $4}‘| cut -d : -f 2`

            if [ "$WebPort" = "80" ];then 

                  echo "you nginx start"

                  exit 0

               else        

                    service nginx start

                    exit 1

            fi

当然还有好多方法,也在不断学习和摸索中。(上述脚本对于数据库等其他服务监控也可以)


linux运维学习shell脚本监控nginx服务

标签:监控nginx   linux运维   shell脚本学习   

原文地址:http://birdcai.blog.51cto.com/11216068/1745222

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!