2009-06-23 56 views
0

我有一個服務,具體如下:蟒蛇服務重新啓動時(編譯EXE)

""" 
The most basic (working) CherryPy 3.1 Windows service possible. 
Requires Mark Hammond's pywin32 package. 
""" 

import cherrypy 
import win32serviceutil 
import win32service 
import sys 
import __builtin__ 

__builtin__.theService = None 

class HelloWorld: 
    """ Sample request handler class. """ 

    def __init__(self): 
     self.iVal = 0 

    @cherrypy.expose 
    def index(self): 

     try: 

      self.iVal += 1 

      if self.iVal == 5: 
       sys.exit() 
      return "Hello world! " + str(self.iVal) 

     except SystemExit: 
      StopServiceError(__builtin__.theService) 


class MyService(win32serviceutil.ServiceFramework): 
    """NT Service.""" 

    _svc_name_ = "CherryPyService" 
    _svc_display_name_ = "CherryPy Service" 
    _svc_description_ = "Some description for this service" 

    def SvcDoRun(self): 
     __builtin__.theService = self 
     StartService() 


    def SvcStop(self): 
     StopService(__builtin__.theService) 


def StartService(): 

    cherrypy.tree.mount(HelloWorld(), '/') 

    cherrypy.config.update({ 
     'global':{ 
      'tools.log_tracebacks.on': True, 
      'log.error_file': '\\Error_File.txt', 
      'log.screen': True, 
      'engine.autoreload.on': False, 
      'engine.SIGHUP': None, 
      'engine.SIGTERM': None 
      } 
     }) 

    cherrypy.engine.start() 
    cherrypy.engine.block() 


def StopService(classObject): 
    classObject.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
    cherrypy.engine.exit() 
    classObject.ReportServiceStatus(win32service.SERVICE_STOPPED) 


def StopServiceError(classObject): 
    classObject.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
    cherrypy.engine.exit() 
    classObject.ReportServiceStatus(serviceStatus=win32service.SERVICE_STOPPED, win32ExitCode=1, svcExitCode=1) 

if __name__ == '__main__': 
    win32serviceutil.HandleCommandLine(MyService) 

我希望Windows重新啓動時sys.ext()導致服務退出服務。有誰知道如何讓它做到這一點?

+0

試試這個問題的答案:[http://stackoverflow.com/questions/220382/how-can-a-windows-service-以編程方式重新啓動本身](http://stackoverflow.com/questions/220382/how-can-a-windows-service-programmatically-restart-itself) – 2009-06-23 10:30:10

回答

4

非編程相關的選項:

Windows服務可以配置爲恢復。選擇service properties窗口的recovery標籤。在第一次,第二次或後續故障後,您可以選擇Restart the Service

一個簡單的想法 - 爲什麼不放棄調用sys.exit()?這種方式服務只是繼續運行,你不必處理重新啓動它。如果您真的需要知道何時self.iVal達到5,您可以報告給事件記錄器(也許重置計數器)。

+0

我已經這樣做,但它仍然不會重新啓動服務一分鐘後。 – williamtroup 2009-06-23 10:54:06

+0

在系統/服務控制管理器中查找事件日誌項目中的線索。 – gimel 2009-06-23 11:15:18

2

我有完全相同的問題:嘗試使用基於Python的服務退出並顯示錯誤代碼,以便服務框架可以重新啓動它。我嘗試了ReportServiceStatus(win32service.SERVICE_STOP_PENDING, win32ExitCode=1, svcExitCode=1)以及sys.exit(1)的方法,但沒有一個提示Windows重新啓動服務,儘管後者在事件日誌中顯示爲錯誤。我結束了不依賴於服務框架,只是這樣做:

def SvcDoRun(self): 
    restart_required = run_service() # will return True if the service needs 
            # to be restarted 
    if restart_required: 
     subprocess.Popen('sleep 5 & sc start %s' % self._svc_name_, shell=True)