zoukankan      html  css  js  c++  java
  • python脚本监听nginx是否运行

    import sys
    import time
    import os
    import logging
    from logging.handlers import RotatingFileHandler
    import config
    
    class Nginx_listen(object):
        logger = None
        @property
        def setup_log(self):
            return self.logger
        @setup_log.setter
        def setup_log(self,log_name):
            """创建日志配置"""
            logger = logging.getLogger(log_name)
            log_path = os.path.join(config.log_path,log_name)
            logger.setLevel(logging.INFO)
            file_handler = RotatingFileHandler(
                log_path, 'a', config.MAX_BYTE, config.BACKUP_COUNT
            )
            file_handler.setFormatter(
                logging.Formatter(
                    "[%(asctime)s] [%(process)d] [%(levelname)s] - %(module)s.%(funcName)s (%(filename)s:%(lineno)d) - %(message)s"
                )
            )
            logger.addHandler(file_handler)
            self.logger = logger
        @staticmethod
        def write_pid(pid):
            """nginx的进程id写入文件"""
            with open(config.pid_path,'w') as fp:
                fp.write("%s
    "%pid)
        @staticmethod
        def read_pid():
            """读取保存文件内nginx的进程id"""
            with open(config.pid_path, 'r') as fp:
                pid = fp.read().strip()
            return pid
        def check_nginx_pid(self):
            """获取启动nginx 的进程id"""
            current_pid = os.popen("sudo pgrep -lo nginx |grep -v grep|awk '{print $1}'").read()
            if current_pid:
                self.write_pid(current_pid)
            return current_pid
    
        def check_nginx_run(self):
            """检查nginx是否启动"""
            status = False
            nginx_pid = self.check_nginx_pid()
            nginx_start_data = os.popen("sudo ps aux|grep nginx |grep -v grep|awk 'NR==1{print $9}'").read()
            nginx_path = os.popen("sudo ps aux|grep nginx |grep -v grep|awk 'NR==1{print $14}'").read()
            current_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
            if nginx_pid and nginx_path:
                self.logger.info("current_time: %s;startime: %s;nginx_path: %s;pid: %s" %
                                 (current_date,nginx_start_data,nginx_path,nginx_pid)
                                 )
                status = True
            else:
                dead_pid = self.read_pid()
                self.logger.warning("current_time: %s; nginx is dead, nginx pid is %s try restart nginx." %
                                    (current_date,dead_pid)
                                    )
            return status
        def start_nginx(self):
            """启动nginx"""
            cmd = "/usr/sbin/nginx"
            cmd_result = os.system(cmd)
            if cmd_result == 0:
                self.check_nginx_run()
            else:
                self.logger.error("start nginx error")
        @staticmethod
        def main(log_name):
            """入口文件"""
            nginx_obj = Nginx_listen()
            nginx_obj.setup_log = log_name
            result = nginx_obj.check_nginx_run()
            if result:
                sys.exit(0)
            else:
                nginx_obj.start_nginx()
    log_name = "nginx"
    nginx_obj = Nginx_listen().main(log_name)
    
  • 相关阅读:
    uip源码剖析【一】——【网络层】ARP解读
    MySql字符编码详解
    51单片机+uip实战
    dos中如何查找一个字符串是否包含在某个文件中,如果有则将该文件名输出
    Full TCP/IP for 8Bit Architectures 阅读
    个人PKM之路
    Overlooked Essentials For Optimizing Code
    Would it be faster to batch SetVertex/PixelShaderConstant calls?
    2D Skinned Mesh(3D的完全翻版 带旋转)
    The difference between d8&d9's constants def in asm shaders
  • 原文地址:https://www.cnblogs.com/xujunkai/p/12374312.html
Copyright © 2011-2022 走看看