前言:
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
新聞熱點
疑難解答