Monitor the process state of the. Py script with a Python script and implement an interrupt restart.

Source: Internet
Author: User
Tags stdin python script

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.