zl程序教程

您现在的位置是:首页 >  后端

当前栏目

linux系统使用python监控apache服务器进程脚本分享

2023-06-13 09:15:16 时间

crtrl.py监控Apache服务器进程的Python脚本

复制代码代码如下:


!/usr/bin/envPython
importos,sys,time

whileTrue:
time.sleep(4)
try:
ret=os.popen("ps-Capache-opid,cmd").readlines()
iflen(ret)<2:
print"apache进程异常退出,4秒后重新启动"
time.sleep(3)
os.system("serviceapache2restart")
except:
print"Error",sys.exc_info()[1]

设置文件权限为执行属性(使用命令chmod+xcrtrl.py),然后加入到/etc/rc.local即可,一旦Apache服务器进程异常退出,该脚本自动检查并且重启。简单说明一下清单5这个脚本不是基于/proc伪文件系统的,是基于Python自己提供的一些模块来实现的。这里使用的是Python的内嵌time模板,time模块提供各种操作时间的函数。