zi663227 发表于 2017-5-1 13:15:45

用Python写个进程监控程序

  http://www.zaoxue.com/article/tech-49312.htm
  有个应用程序不是很稳定,又暂时找到具体问题,只知道出现错误时日志文件会反映出来.
该应用定时更新日志目录,有两个日志文件:fromclient.log 记录接收请求,fromserver.log记录接收服务端返回.

出现问题时一般是fromclient.log日志在更新,但是fromserver.log就停止了.
灵机一动,何不用python写个监控日志的程序,发现程序异常就自动重启,这样不至于严重影响客户使用.
多出时间来彻底解决问题.

于是就有了下面的代码:
#!/bin/env Python
# -*- coding: cp936 -*-

  import glob,os,time,stat,sys
  deadflag = 60   #判断进程死掉的秒数
  def GetFileTime(filename):
  return os.stat(filename)
  def main():
  path = glob.glob("../log/PROXY*")[-1]
  clifile = path + "/" + "mt.log"
  svrfile = path + "/" + "fromserver.log"
  clitime = GetFileTime(clifile)
  svrtime = GetFileTime(svrfile)
  print clifile,time.localtime(clitime)
  print svrfile,time.localtime(svrtime)
  if abs(svrtime-clitime) > deadflag:
  print "time is over! will restart!"
  os.system("sh /home/esm/bin/restart_tcpproxy.sh")
  return
  if __name__ == "__main__":
  main()
页: [1]
查看完整版本: 用Python写个进程监控程序