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\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是否运行的更多相关文章
- python hook监听事件
python hook监听事件 作者:vpoet mail:vpoet_sir@163.com # -*- coding: utf-8 -*- # # by oldj http://oldj.net/ ...
- 使用python脚本批量设置nginx站点的rewrite规则
一般情况下,配置rewrite重写规则使用shell脚本即可: 把url拼凑成1,2文件中中的格式,运行 chongxie.sh 即可生成我们需要的rewrite规则 [root@web01:/opt ...
- python 文件监听
对文件进行监听.过滤 def tail(filename): f = open(file=filename, mode='r', encoding='utf-8') # 打开文件不能用with,因为监 ...
- python socketserver监听多端口多进程
多进程监听多端口 # 多线程socket # 程序监听两个端口,端口逻辑相同其中一个端口放在子进程下 # 每次请求会在产生一个进程处理请求 import SocketServer from multi ...
- Python——pyHook监听鼠标键盘事件
pyHook包为Windows中的全局鼠标和键盘事件提供回调. 底层C库报告的信息包括事件的时间,事件发生的窗口名称,事件的值,任何键盘修饰符等. 而正常工作需要pythoncom等操作系统的API的 ...
- python pynput监听键盘
"""小白随笔,大佬勿喷""" #键盘输入 from pynput.keyboard import Key,Controller,Liste ...
- Linux开机自启应用&开机执行脚本&监听端口应用挂掉了执行启动脚本
linux开机自启 背景 目前要部署一个spring boot框架的jar包,实现开机启动项目或者应用挂掉了 执行启动脚本 在root目录下有一个启动项目的脚本: app_start.sh app_s ...
- python 脚本在linux环境下运行
有两种方式:1.直接使用python xxxx.py执行.其中python可以写成python的绝对路径.使用which python进行查询.2.在文件的头部(第一行)写上#!/usr/bin/py ...
- zabbix-agent python脚本侦听服务器异常登录,并告警
py脚本 import re,subprocess,time,datetime #gpasswd -a zabbix adm def ftime(a): a = a.replace('Jan','01 ...
随机推荐
- 【转载】Java的JDBC事务详解
转自:http://www.cnblogs.com/azhqiang/p/4044127.html 事务的特性: 1) 原子性(atomicity):事务是数据库的逻辑工作单位,而且是必须是原子工作单 ...
- MP4转mp3
python实现: 依赖: glob,pydub "Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not wo ...
- Mac系统升级后git没法使用的问题
今天更新了mac系统(Catalina)发现之前的项目没法提交,然后大概查了一下啊mac更新方面的问题 mac每次更新时系统就会卸载xcode 在终端输入 xcode-select --instal ...
- WKWebView 使用的坑
WKWebView 简介: WKWebView 是苹果在 WWDC 2014 上推出的新一代 webView 组件,用以替代 UIKit 中笨重难用.内存泄漏的 UIWebView.WKWebView ...
- Eclipse导入git上的maven web项目 以及部署成功运行
在公司开发的时候都是用maven 以及git环境 开发的环境,那么我们初学者怎么将公司的项目成功导入到eclipse中,并且成功运行那???下面的教程就可以告诉大家~ (ps:第二步可能是大家会遇 ...
- oracle 唯独测试视图
--建立用户分配权限 create user groper identified by groper / grant connect,resource to groper / grant create ...
- 国际化支持、activity生命周期、屏幕翻转的ui适配
国际化 对于手机的不同语言做出不同的语言描述,这里只是简单的提一下,实际上针对比较大型的项目,有可能不同的语言要做不同的ui适配. 例如下面:中文长度不长但是在德语中占据很长的位置,这个就要针对性的适 ...
- 猴博士4小时讲完C语言视频教程
猴博士4小时讲完C语言视频教程,一共有9节课. 目录结构如下: 目录:/2020030-猴博士4小时讲完C语言 [1G] ┣━━1.C语言基本语句(上)(更多资源访问:www.jimeng365.cn ...
- 读书笔记 - 把时间当作朋友 by 李笑来
要管理的不是时间,而是自己. 摸着石头渐行渐远,最终也能过河.- 朱敏 赛伯乐(中国)投资公司 董事长 一切都靠积累,一切都可提前准备,越早醒悟越好.人的一生是奋斗的一生,但是有的人一生过得很伟大,有 ...
- bm坏字符 , Horspool算法 以及Sunday算法的不同
bm坏字符 , Horspool算法 以及Sunday算法的不同 一.bm中的坏字符规则思想 (1)模式串与主串从后向前匹配 (2)发现坏字符后,如果坏字符不存在于模式串中:将模式串的头字符与坏字符后 ...