最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python寫的服務(wù)監(jiān)控程序?qū)嵗?/h1>
 更新時(shí)間:2015年01月31日 14:17:13   投稿:junjie  
這篇文章主要介紹了Python寫的服務(wù)監(jiān)控程序?qū)嵗?本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下

前言:

Redhat下安裝Python2.7

rhel6.4自帶的是2.6, 發(fā)現(xiàn)有的機(jī)器是python2.4。 到python網(wǎng)站下載源代碼,解壓到Redhat上,然后運(yùn)行下面的命令:

復(fù)制代碼 代碼如下:

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

這樣安裝之后默認(rèn)不會(huì)啟用Python2.7,需要使用/usr/local/python27/bin/python2.7調(diào)用新版本的python。

而下面的安裝方式會(huì)直接接管現(xiàn)有的python

復(fù)制代碼 代碼如下:

# ./configure
# make
# make install

開始:

服務(wù)子進(jìn)程被監(jiān)控主進(jìn)程創(chuàng)建并監(jiān)控,當(dāng)子進(jìn)程異常關(guān)閉,主進(jìn)程可以再次啟動(dòng)之。使用了python的subprocess模塊。就這個(gè)簡單的代碼,居然互聯(lián)網(wǎng)上沒有現(xiàn)成可用的例子。沒辦法,我寫好了貢獻(xiàn)出來吧。

首先是主進(jìn)程代碼:service_mgr.py

復(fù)制代碼 代碼如下:

#!/usr/bin/python 
#-*- coding: UTF-8 -*- 
# cheungmine 
# stdin、stdout和stderr分別表示子程序的標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤。 
#  
# 可選的值有: 
#   subprocess.PIPE - 表示需要?jiǎng)?chuàng)建一個(gè)新的管道. 
#   一個(gè)有效的文件描述符(其實(shí)是個(gè)正整數(shù)) 
#   一個(gè)文件對(duì)象 
#   None - 不會(huì)做任何重定向工作,子進(jìn)程的文件描述符會(huì)繼承父進(jìn)程的. 
#  
# stderr的值還可以是STDOUT, 表示子進(jìn)程的標(biāo)準(zhǔn)錯(cuò)誤也輸出到標(biāo)準(zhǔn)輸出. 
#  
# subprocess.PIPE 
# 一個(gè)可以被用于Popen的stdin、stdout和stderr 3個(gè)參數(shù)的特輸值,表示需要?jiǎng)?chuàng)建一個(gè)新的管道. 
#  
# subprocess.STDOUT 
# 一個(gè)可以被用于Popen的stderr參數(shù)的特輸值,表示子程序的標(biāo)準(zhǔn)錯(cuò)誤匯合到標(biāo)準(zhǔn)輸出. 
################################################################################ 
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, # 表示子進(jìn)程的標(biāo)準(zhǔn)錯(cuò)誤也輸出到標(biāo)準(zhǔn)輸出 
                stdout=subprocess.PIPE    # 表示需要?jiǎng)?chuàng)建一個(gè)新的管道 
            ) 
        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"]) 

然后是子進(jìn)程代碼:testpipe.py

復(fù)制代碼 代碼如下:

#!/usr/bin/python 
#-*- coding: UTF-8 -*- 
# cheungmine 
# 模擬一個(gè)woker進(jìn)程,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上運(yùn)行很簡單:

復(fù)制代碼 代碼如下:

$ python service_mgr.py

Windows上以后臺(tái)進(jìn)程運(yùn)行:
復(fù)制代碼 代碼如下:

> start pythonw service_mgr.py

代碼中需要修改:
復(fù)制代碼 代碼如下:

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

相關(guān)文章

  • 返回最大值的index pytorch方式

    返回最大值的index pytorch方式

    這篇文章主要介紹了返回最大值的index pytorch方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • python卸載后再次安裝遇到的問題解決

    python卸載后再次安裝遇到的問題解決

    這篇文章主要給大家介紹了關(guān)于python卸載后再次安裝遇到問題解決的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python正則表達(dá)式re.sub()用法詳解

    Python正則表達(dá)式re.sub()用法詳解

    re.sub用于替換字符串中的匹配項(xiàng),下面這篇文章主要給大家介紹了關(guān)于Python正則表達(dá)式re.sub()用法的相關(guān)資料,文中通過實(shí)例代碼以及圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • 解決Python發(fā)送Http請(qǐng)求時(shí),中文亂碼的問題

    解決Python發(fā)送Http請(qǐng)求時(shí),中文亂碼的問題

    這篇文章主要介紹了解決Python發(fā)送Http請(qǐng)求時(shí),中文亂碼的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python3 traceback模塊來追蹤和打印異常信息

    python3 traceback模塊來追蹤和打印異常信息

    本文主要介紹了python3 traceback模塊來追蹤和打印異常信息,這個(gè)模塊提供了許多函數(shù)來處理異常,并且能夠輸出異常的堆棧信息,感興趣的可以了解一下
    2024-02-02
  • python lambda的使用詳解

    python lambda的使用詳解

    這篇文章主要介紹了python lambda的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-02-02
  • python數(shù)字圖像處理之骨架提取與分水嶺算法

    python數(shù)字圖像處理之骨架提取與分水嶺算法

    這篇文章主要介紹了python數(shù)字圖像處理之骨架提取與分水嶺算法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • 基于Keras的擴(kuò)展性使用

    基于Keras的擴(kuò)展性使用

    這篇文章主要介紹了Keras的擴(kuò)展性使用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 從零學(xué)Python之hello world

    從零學(xué)Python之hello world

    從今天開始講陸續(xù)發(fā)布一系列python基礎(chǔ)教程,讓新手更快更好的入門。
    2014-05-05
  • python爬蟲利用代理池更換IP的方法步驟

    python爬蟲利用代理池更換IP的方法步驟

    這篇文章主要介紹了python爬蟲利用代理池更換IP的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02

最新評(píng)論

瑞安市| 延寿县| 遂溪县| 青河县| 舞阳县| 易门县| 武强县| 科技| 呼和浩特市| 清远市| 蓬溪县| 湘阴县| 兴国县| 花垣县| 景东| 青龙| 南木林县| 新田县| 慈溪市| 灵川县| 布拖县| 铁力市| 重庆市| 潍坊市| 米易县| 汉沽区| 友谊县| 文成县| 宜良县| 桂林市| 威信县| 尉犁县| 河东区| 房山区| 新建县| 新田县| 枣阳市| 光山县| 中牟县| 开原市| 玛纳斯县|