Python寫的服務監控程式執行個體

來源:互聯網
上載者:User
前言:

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"])
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.