加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

写了个监控nginx进程的Python脚本

发布时间:2020-12-16 19:38:19 所属栏目:Python 来源:网络整理
导读:复制代码 代码如下: #!/usr/bin/env python import os,sys,time while True: time.sleep(3) try: ret = os.popen('ps -C nginx -o pid,cmd').readlines() if len(ret) 2: print "nginx process killed,restarting service in 3 seconds." time.sleep(3) os.s
复制代码 代码如下:

#!/usr/bin/env python
import os,sys,time

while True:
time.sleep(3)
try:
ret = os.popen('ps -C nginx -o pid,cmd').readlines()
if len(ret) < 2:
print "nginx process killed,restarting service in 3 seconds."
time.sleep(3)
os.system("service nginx restart")
except:
print "Error",sys.exc_info()[1]

设置文件可执行属性,加入到/etc/rc.local,总算放心了。
这种方法还可以监控别的进程,我相信应该有现成的监控软件,但是我觉得写个脚本更方便。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读