crtrl.py監(jiān)控Apache服務(wù)器進(jìn)程的Python 腳本
復(fù)制代碼 代碼如下:
!/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 進(jìn)程異常退出, 4 秒后重新啟動(dòng)"
time.sleep(3)
os.system("service apache2 restart")
except:
print "Error", sys.exc_info()[1]
設(shè)置文件權(quán)限為執(zhí)行屬性(使用命令 chmod +x crtrl.py),然后加入到/etc/rc.local 即可,一旦 Apache 服務(wù)器進(jìn)程異常退出,該腳本自動(dòng)檢查并且重啟。 簡(jiǎn)單說明一下清單 5 這個(gè)腳本不是基于/proc 偽文件系統(tǒng)的,是基于 Python 自己提供的一些模塊來實(shí)現(xiàn)的 。這里使用的是 Python 的內(nèi)嵌 time 模板,time 模塊提供各種操作時(shí)間的函數(shù)。
新聞熱點(diǎn)
疑難解答
圖片精選