写一个python的服务监控程序

前言:

Redhat下安装Python2.7

rhel6.4自带的是2.6, 发现有的机器是python2.4。 到python网站下载源代码,解压到Redhat上,然后运行下面的命令:

# ./configure --prefix=/usr/local/python27

# make

# make install

这样安装之后默认不会启用Python2.7,需要使用/usr/local/python27/bin/python2.7调用新版本的python。

而下面的安装方式会直接接管现有的python

# ./configure

# make

# make install

开始:

服务子进程被监控主进程创建并监控,当子进程异常关闭,主进程可以再次启动之。使用了python的subprocess模块。就这个简单的代码,居然互联网上没有现成可用的例子。没办法,我写好了贡献出来吧。

首先是主进程代码:service_mgr.py

#!/usr/bin/python
#-*- coding: UTF-8 -*-
# cheungmine
# stdin、stdout和stderr分别表示子程序的标准输入、标准输出和标准错误。
#
# 可选的值有:
#   subprocess.PIPE - 表示需要创建一个新的管道.
#   一个有效的文件描述符(其实是个正整数)
#   一个文件对象
#   None - 不会做任何重定向工作,子进程的文件描述符会继承父进程的.
#
# stderr的值还可以是STDOUT, 表示子进程的标准错误也输出到标准输出.
#
# subprocess.PIPE
# 一个可以被用于Popen的stdin、stdout和stderr 3个参数的特输值,表示需要创建一个新的管道.
#
# subprocess.STDOUT
# 一个可以被用于Popen的stderr参数的特输值,表示子程序的标准错误汇合到标准输出.
################################################################################
import os
import sys
import getopt

import time
import datetime

import codecs

import optparse
import ConfigParser

import signal
import subprocess
import select

# logging
# require python2.6.6 and later
import logging
from logging.handlers import RotatingFileHandler

## log settings: SHOULD BE CONFIGURED BY config
LOG_PATH_FILE = "./my_service_mgr.log"
LOG_MODE = 'a'
LOG_MAX_SIZE = 4*1024*1024 # 4M per file
LOG_MAX_FILES = 4          # 4 Files: my_service_mgr.log.1, printmy_service_mgrlog.2, ...
LOG_LEVEL = logging.DEBUG  

LOG_FORMAT = "%(asctime)s %(levelname)-10s[%(filename)s:%(lineno)d(%(funcName)s)] %(message)s"  

handler = RotatingFileHandler(LOG_PATH_FILE, LOG_MODE, LOG_MAX_SIZE, LOG_MAX_FILES)
formatter = logging.Formatter(LOG_FORMAT)
handler.setFormatter(formatter)

Logger = logging.getLogger()
Logger.setLevel(LOG_LEVEL)
Logger.addHandler(handler) 

# color output
#
pid = os.getpid() 

def print_error(s):
    print '\033[31m[%d: ERROR] %s\033[31;m' % (pid, s)

def print_info(s):
    print '\033[32m[%d: INFO] %s\033[32;m' % (pid, s)

def print_warning(s):
    print '\033[33m[%d: WARNING] %s\033[33;m' % (pid, s)

def start_child_proc(command, merged):
    try:
        if command is None:
            raise OSError, "Invalid command"

        child = None

        if merged is True:
            # merge stdout and stderr
            child = subprocess.Popen(command,
                stderr=subprocess.STDOUT, # 表示子进程的标准错误也输出到标准输出
                stdout=subprocess.PIPE    # 表示需要创建一个新的管道
            )
        else:
            # DO NOT merge stdout and stderr
            child = subprocess.Popen(command,
                stderr=subprocess.PIPE,
                stdout=subprocess.PIPE)

        return child

    except subprocess.CalledProcessError:
        pass # handle errors in the called executable
    except OSError:
        pass # executable not found

    raise OSError, "Failed to run command!"

def run_forever(command):
    print_info("start child process with command: " + ' '.join(command))
    Logger.info("start child process with command: " + ' '.join(command))

    merged = False
    child = start_child_proc(command, merged)

    line = ''
    errln = ''

    failover = 0

    while True:
        while child.poll() != None:
            failover = failover + 1
            print_warning("child process shutdown with return code: " + str(child.returncode))
            Logger.critical("child process shutdown with return code: " + str(child.returncode))

            print_warning("restart child process again, times=%d" % failover)
            Logger.info("restart child process again, times=%d" % failover)
            child = start_child_proc(command, merged)

        # read child process stdout and log it
        ch = child.stdout.read(1)
        if ch != '' and ch != '\n':
            line += ch
        if ch == '\n':
            print_info(line)
            line = ''

        if merged is not True:
            # read child process stderr and log it
            ch = child.stderr.read(1)
            if ch != '' and ch != '\n':
                errln += ch
            if ch == '\n':
                Logger.info(errln)
                print_error(errln)
                errln = ''

    Logger.exception("!!!should never run to this!!!")  

if __name__ == "__main__":
    run_forever(["python", "./testpipe.py"])

然后是子进程代码:testpipe.py

#!/usr/bin/python
#-*- coding: UTF-8 -*-
# cheungmine
# 模拟一个woker进程,10秒挂掉
import os
import sys

import time
import random

cnt = 10

while cnt >= 0:
    time.sleep(0.5)
    sys.stdout.write("OUT: %s\n" % str(random.randint(1, 100000)))
    sys.stdout.flush()

    time.sleep(0.5)
    sys.stderr.write("ERR: %s\n" % str(random.randint(1, 100000)))
    sys.stderr.flush()

    #print str(cnt)
    #sys.stdout.flush()
    cnt = cnt - 1

sys.exit(-1)

Linux上运行很简单:

$ python service_mgr.py

Windows上以后台进程运行:

> start pythonw service_mgr.py

代码中需要修改:

run_forever(["python", "testpipe.py"])

写一个python的服务监控程序的更多相关文章

  1. tensorflow学习笔记3:写一个mnist rpc服务

    本篇做一个没有实用价值的mnist rpc服务,重点记录我在调试整合tensorflow和opencv时遇到的问题: 准备模型 mnist的基础模型结构就使用tensorflow tutorial给的 ...

  2. 如何手动写一个Python脚本自动爬取Bilibili小视频

    如何手动写一个Python脚本自动爬取Bilibili小视频 国庆结束之余,某个不务正业的码农不好好干活,在B站瞎逛着,毕竟国庆嘛,还让不让人休息了诶-- 我身边的很多小伙伴们在朋友圈里面晒着出去游玩 ...

  3. 写一个Python的windows服务

    1. 安装pywin32和pyinstaller pip install pywin32 pip install pyinstaller 2.写一个服务Demo # -*- coding: utf-8 ...

  4. 写一个python脚本监控在linux中的进程

    在虚拟机中安装Linux中的CentOS7系统 https://baijiahao.baidu.com/s?id=1597320700700593557&wfr=spider&for= ...

  5. 写一个Python 1、通过select实现的最简单的web框架2、通过wsgiref实现的web框架

    #!/usr/bin/env python # -*- coding: utf- -*- import socket import select class MyRequest: "&quo ...

  6. 写一个python 爬虫爬取百度电影并存入mysql中

    目标是利用python爬取百度搜索的电影 在类型 地区 年代各个标签下 电影的名字 评分 和图片连接 以及 电影连接 首先我们先在mysql中建表 create table liubo4( id in ...

  7. 如何写一个Python万能装饰器,既可以装饰有参数的方法,也可以装饰无参数方法,或者有无返回值都可以装饰

    Python中的装饰器,可以有参数,可以有返回值,那么如何能让这个装饰器既可以装饰没有参数没有返回值的方法,又可以装饰有返回值或者有参数的方法呢?有一种万能装饰器,代码如下: def decorate ...

  8. 写一个python小程序

    在windows环境下进行操作 window+R 输入cmd  创建一个文件夹 mkdir pytxt 创建一个py文件 py.py  用notepad或者记事本等工具进行编辑 或 首先声明pytho ...

  9. 手写一个python迭代器

    分析 我们都知道一个可迭代对象可以通过iter()可以返回一个迭代器. 如果想要一个对象称为可迭代对象,即可以使用for,那么必须实现__iter __()方法. 在一个类的实例对象想要变成迭代器,就 ...

随机推荐

  1. [Vim]vim学习笔记--多个文件打开,切换,关闭

    一种情况是在shell中用vim打开多个文件,另一种是在vim编辑器中打开多个文件 同时打开多个文件 vim file1 file2  打开文件并水平窗口显示 vim -o file1 file2 打 ...

  2. EJB开发第一个无状态会话bean、开发EJB客户端

    开发第一个无状态会话bean EJB中的三中bean: 会话Bean(Session Bean) 负责与客户端交互,是编写业务逻辑的地方,在会话bean中可以通过JDBC直接操作数据库,但大多数情况下 ...

  3. android viewpager切换到最后一页时,跳转至其他activity

    许许多多的qpp,在启动时会显示一些帮助页或者产品介绍什么的,通常滑动到最后一页时会有一个开始体验的按钮,用来跳转到app的主activity上,但是如果没有? 可以通过如下方法实现: 1.为View ...

  4. 【DevOps敏捷开发动手实验】开源文档 v2015.2 stable 版发布

    Team Foundation Server 2015 Update 2版本终于在2周前的//Build 2016大会上正式发布了,借这个东风,小编也完成了[DevOps敏捷开发动手实验]开源文档的第 ...

  5. 百度地图SDK3.4的使用

    使用过百度地图的开发者应该都知道原始百度地图的开发的基本流程,但是随着百度地图的更新,百度地图的api有了翻天覆地的变化,最新版本的sdk为v3.4 2015年4月14日上线,优化了许多接口的设计,简 ...

  6. 所谓 Spinner 组件

    Spinner组件在平常的开发过程中应该算是比较常用的隶属于Input Control的android控件了,所以我们很有必要掌握这一个知识点,下面我就来谈一谈我对这个组件的一些认识. 是什么? 下拉 ...

  7. 【java集合框架源码剖析系列】java源码剖析之TreeSet

    本博客将从源码的角度带领大家学习TreeSet相关的知识. 一TreeSet类的定义: public class TreeSet<E> extends AbstractSet<E&g ...

  8. 查看LOV对应查询语句的研究

    一.获取当前会话id 1.方法一 tools: Help > About 2.方法二 打开个性化定义界面(如果没有权限,到系统配置文件设置中,查看是否是"隐藏诊断菜单"被设置 ...

  9. JPA(三)之实体关系一对多(多对一)

     1.背景介绍: 对于购买商品时,订单信息(Order)和订单商品信息(OrderItem)的关系就是一对多的关系. 2.实体bean: Order.java代码 ? 1 2 3 4 5 6 7 ...

  10. SpringMVC源码分析--容器初始化(三)HttpServletBean

    在上一篇博客springMVC源码分析--容器初始化(二)DispatcherServlet中,我们队SpringMVC整体生命周期有一个简单的说明,并没有进行详细的源码分析,接下来我们会根据博客中提 ...