用Python脚本监测.py脚本的进程状态,并实现中断重启。,python.py,用Python脚本监


用Python脚本监测.py脚本的执行状态,并实现中断重启。#!/usr/bin/python#-*-coding:utf-8-*-importsubprocess,time,sysTIME=10#程序状态检测间隔(单位:分钟)CMD="get_bjipku.py"#需要执行程序的绝对路径,支持jar如:D:\\calc.exe或者D:\\test.jarclassAuto_Run():def__init__(self,sleep_time,cmd):self.sleep_time=sleep_timeself.cmd=cmdself.ext=(cmd[-3:]).lower()#判断文件的后缀名,全部换成小写self.p=None#self.p为subprocess.Popen()的返回值,初始化为Noneself.run()#启动时先执行一次程序try:while1:time.sleep(sleep_time*6)#休息10分钟,判断程序状态self.poll=self.p.poll()#判断程序进程是否存在,None:表示程序正在运行其他值:表示程序已退出ifself.pollisNone:print"运行正常"else:print"未检测到程序运行状态,准备启动程序"self.run()exceptKeyboardInterruptase:print"检测到CTRL+C,准备退出程序!"#self.p.kill()#检测到CTRL+C时,kill掉CMD中启动的exe或者jar程序defrun(self):ifself.ext==".py":print‘startOK!‘self.p=subprocess.Popen([‘python‘,‘%s‘%self.cmd],stdin=sys.stdin,stdout=sys.stdout,stderr=sys.stderr,shell=False)else:passapp=Auto_Run(TIME,CMD)


本文出自 “全球互联云主机Q874247458” 博客,请务必保留此出处http://gosweet.blog.51cto.com/11759495/1914444

用Python脚本监测.py脚本的进程状态,并实现中断重启。

评论关闭