Monitor the execution state of the. Py script with a Python script and implement an interrupt restart. #!/usr/bin/python# -*- coding:utf-8 -*-import subprocess,time,systime = 10 #程序状态检测间隔 (units: minutes) cmd = "get_bjipku.py" #需要执行程序的绝对路径, support jar such as: D:\\calc.exe or D:\\test.jarclass auto_run (): def __init__ (self,sleep_time,cmd): self.sleep_time = sleep_time self.cmd = cmd self.ext = (cmd[-3:]). Lower () #判断文件的后缀名, change all to lowercase self.p = none # SELF.P is the return value of Subprocess.popen (), initialized to None self.run () #启动时先执行一次程序 try: while 1: time.sleep (SLEEP_TIME * 6) #休息10分钟, judging program status self.poll = self.p.poll () #判断程序进程是否存在, None: Indicates that the program is running Other value: Indicates that the program has exited if self.poll is None: print "normal operation" else: print "No program running status detected, ready to Start program" self.run () except KeyboardInterrupt as e: print "Detected CTRL + C, ready to exit the program!" # self.p.kill () when #检测到CTRL +c, Kill the EXE or JAR program started in cmd def run (self): if self.ext == ". Py": print ' start ok! ' 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)
This article is from the global connected Cloud host Q874247458 blog, so be sure to keep this source http://gosweet.blog.51cto.com/11759495/1914444
Monitor the process state of the. Py script with a Python script and implement an interrupt restart.