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 模块提供各种操作时间的函数。