crtrl.py監控Apache服務器進程的Python 腳本
復制代碼 代碼如下:
!/usr/bin/env Python
import os, sys, time
while True:
time.sleep(4)
try:
ret = os.popen('ps -C apache -o pid,cmd').readlines()
if len(ret) < 2:
print "apache 進程異常退出, 4 秒后重新啟動"
time.sleep(3)
os.system("service apache2 restart")
except:
print "Error", sys.exc_info()[1]
設置文件權限為執行屬性(使用命令 chmod +x crtrl.py),然后加入到/etc/rc.local 即可,一旦 Apache 服務器進程異常退出,該腳本自動檢查并且重啟。 簡單說明一下清單 5 這個腳本不是基于/proc 偽文件系統的,是基于 Python 自己提供的一些模塊來實現的 。這里使用的是 Python 的內嵌 time 模板,time 模塊提供各種操作時間的函數。
新聞熱點
疑難解答