python脚本监控进程,服务停止后自动启动

版权声明:原创文章,欢迎转载但请备注来源及原文链接 https://blog.csdn.net/levy_cui/article/details/51143226
实例1:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. import subprocess
  3. import datetime
  4. import re
  5. import sys
  6. import os
  7. res = subprocess.Popen("id root",stdout=subprocess.PIPE,shell=True)
  8. output_lines=res.stdout.readlines()
  9. for line in output_lines:
  10.     if not re.search("ma", line):
  11.         line = line.strip()
  12.         dt=datetime.datetime.now()
  13.         fp=open('/root/check_ma_group/check_ma.txt','a')
  14.         fp.write('root in ma group at %s\n' % dt.strftime('%Y-%m-%d %H:%M:%S'))
  15.         fp.close()
  16.         subprocess.Popen("/usr/sbin/usermod -G ma root",shell=True)
  17.     else:
  18.         pass

实例2:

点击(此处)折叠或打开

  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/python
  3.       
  4. import os
  5. import subprocess
  6.       
  7. res = subprocess.Popen('ps -ef | grep ASRS',stdout=subprocess.PIPE,shell=True)
  8. attn=res.stdout.readlines()
  9. counts=len(attn) #获取ASRS下的进程个数
  10. print counts
  11. if counts<10: #当进程不够正常运行的个数时,说明某只程式退出了
  12.     os.system('python /home/pi/ASRS/ASRS.py') ##启动程式
  13.   ##os.system('reboot') #重启系统

实例三:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. import subprocess
  3. import datetime
  4. res = subprocess.Popen(“ps -ef | grep tomcat”,stdout=subprocess.PIPE,shell=True)
  5. tomcats=res.stdout.readlines()
  6. counts=len(tomcats)
  7. if counts<4:
  8.     dt=datetime.datetime.now()
  9.     fp=open(‘/root/tomcat6.txt’,


定时运行该脚本
crontab -e

点击(此处)折叠或打开

  1. */10 * * * * python /root/autorestart-tomcat.py
每十分钟运行该脚本一次
展开阅读全文

没有更多推荐了,返回首页