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\n"%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)

python脚本监听nginx是否运行的更多相关文章

  1. python hook监听事件

    python hook监听事件 作者:vpoet mail:vpoet_sir@163.com # -*- coding: utf-8 -*- # # by oldj http://oldj.net/ ...

  2. 使用python脚本批量设置nginx站点的rewrite规则

    一般情况下,配置rewrite重写规则使用shell脚本即可: 把url拼凑成1,2文件中中的格式,运行 chongxie.sh 即可生成我们需要的rewrite规则 [root@web01:/opt ...

  3. python 文件监听

    对文件进行监听.过滤 def tail(filename): f = open(file=filename, mode='r', encoding='utf-8') # 打开文件不能用with,因为监 ...

  4. python socketserver监听多端口多进程

    多进程监听多端口 # 多线程socket # 程序监听两个端口,端口逻辑相同其中一个端口放在子进程下 # 每次请求会在产生一个进程处理请求 import SocketServer from multi ...

  5. Python——pyHook监听鼠标键盘事件

    pyHook包为Windows中的全局鼠标和键盘事件提供回调. 底层C库报告的信息包括事件的时间,事件发生的窗口名称,事件的值,任何键盘修饰符等. 而正常工作需要pythoncom等操作系统的API的 ...

  6. python pynput监听键盘

    """小白随笔,大佬勿喷""" #键盘输入 from pynput.keyboard import Key,Controller,Liste ...

  7. Linux开机自启应用&开机执行脚本&监听端口应用挂掉了执行启动脚本

    linux开机自启 背景 目前要部署一个spring boot框架的jar包,实现开机启动项目或者应用挂掉了 执行启动脚本 在root目录下有一个启动项目的脚本: app_start.sh app_s ...

  8. python 脚本在linux环境下运行

    有两种方式:1.直接使用python xxxx.py执行.其中python可以写成python的绝对路径.使用which python进行查询.2.在文件的头部(第一行)写上#!/usr/bin/py ...

  9. zabbix-agent python脚本侦听服务器异常登录,并告警

    py脚本 import re,subprocess,time,datetime #gpasswd -a zabbix adm def ftime(a): a = a.replace('Jan','01 ...

随机推荐

  1. Python的深拷贝、浅拷贝

    浅拷贝 定义:浅拷贝只是对另外一个变量的内存地址的拷贝,这两个变量指向同一个内存地址的变量值. 浅拷贝的特点: 公用一个值: 这两个变量的内存地址一样: 对其中一个变量的值改变,另外一个变量的值也会改 ...

  2. 转载:EQ--biquad filter

    http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt https://arachnoid.com/BiQuadDesigner/index.html ...

  3. linux php 扩展安装

    phpize./configure --with-php-config=/usr/bin/php-config make && make install

  4. codeforces 1198B - Welfare State

    题目链接:http://codeforces.com/problemset/status 题目大意为有n个市民,每个市民有ai点数财富,以下有q次操作,操作类型为两类,1类:把第p个市民的财富改为x, ...

  5. unidac 断线重连

    unidac 断线重连 因为物理网络断连或数据库超时踢掉连接等原因,Tuniconnection建立的连接可能会断掉. 在默认状态下,程序会抛出异常框,并罢工. 其实Tuniconnection是可以 ...

  6. 【SIKI学院】愤怒的小鸟创建过程-1

    第一讲:资源导入,场景的简单搭建 1.创建一个2D工程,用到了3个场景,因此ctrl +S创建场景00-level,ctrl+N保存在你创建的文件夹中,这样一个文件就创建好了,之后继续重复此步骤创建另 ...

  7. Bugku-CTF之这是一个神奇的登陆框

    Day32 这是一个神奇的登陆框 http://123.206.87.240:9001/sql/ flag格式flag{}  

  8. 深度学习之tensorflow框架(下)

    def tensor_demo(): """ 张量的演示 :return: """ tensor1 = tf.constant(4.0) t ...

  9. v:bind指令对于传boolean值的注意之处

    1,

  10. CSS学习(3)样式表

    如何插入样式表 插入样式表的方法有三种: 外部样式表(External style sheet) 内部样式表(Internal style sheet) 内联样式(Inline style) 外部样式 ...