2012-07-22 82 views
0

我创建了一个Windows服务在Python(Windows XP中),在这个服务我运行一个Python服务器程序(与p = subprocess.Popen(Arg)),当我注销服务正在运行,但我的应用程序停止。 什么是探针权利,其他? 有人可以帮我吗?应用程序运行从Windows服务在python停止注销时Windows XP

感谢

源代码:

class PythonService(win32serviceutil.ServiceFramework): 
    # you can NET START/STOP the service by the following name 
    _svc_name_ = "PythonService" 
    # this text shows up as the service name in the Service 
    # Control Manager (SCM) 
    _svc_display_name_ = "server service" 
    # this text shows up as the description in the SCM 
    _svc_description_ = "This service run a server" 


    def __init__(self, args): 
     win32serviceutil.ServiceFramework.__init__(self,args) 
     # create an event to listen for stop requests on 
     self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) 
     servicemanager.LogInfoMsg("PythonService Service is starting") 


    # core logic of the service 
    def SvcDoRun(self): 
     rc = None  
     PathPython = "c:/python2.6/python.exe" 
     Script = "c:/PythonService/service.py" 
     sJson = "c:/PythonService/service_static.json" 


     Arg = [PathPython,Script,"-c",sJson] 
     process_arreter = 1 

     try : 
      # run a server 
      p = subprocess.Popen(Arg) 
     except OSError, why: 
      msgerror = "PythonService Service is not running :" + os.strerror(why.errno) + " ["+PathPython+","+Script+","+sJson+"]" 
      servicemanager.LogErrorMsg(msgerror)  
      return 

     # if the stop event hasn't been fired keep looping 
     servicemanager.LogInfoMsg("PythonService Service is running") 
     while rc != win32event.WAIT_OBJECT_0: 
      # block for 5 seconds and listen for a stop event 
      rc = win32event.WaitForSingleObject(self.hWaitStop, 5000) 
      if (p.poll()!=None): 
       if process_arreter: 
        servicemanager.LogWarningMsg("Server-Engine is stopped (failed)") 
       process_arreter = 0 

     if process_arreter: 
      try: 
       p.terminate() 
       servicemanager.LogInfoMsg("Server-Engine is now stopped") 
      except : 
       pass 


    # called when we're being shut down 
    def SvcStop(self): 
     # tell the SCM we're shutting down 
     self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
     # fire the stop event 
     win32event.SetEvent(self.hWaitStop) 
     servicemanager.LogInfoMsg("PythonService Service is stopped") 



if __name__ == '__main__': 
    win32serviceutil.HandleCommandLine(PythonService)` 

回答

0

正在运行的用户服务作为,和它的权限,它的所有需要​​的功能?

服务不会与用户在同一上下文中运行,除非您指定它们具有这些权限。

如果你在一个Windows服务中,你必须指定你的文件路径,并确保它们的路径有权被读取为 ,但它没有完成,我无缘无故地将我的头发拉出来。

+0

George,我的服务以管理员权限运行,python projet安装了管理员权限;我认为必须陷入注销事件,以避免服务器停止。 – louis 2012-07-23 19:56:47

+0

我知道这将是一个奇怪的问题,但确切地说,“你如何开始和停止服务”? – 2012-07-24 06:54:27

0
# first try to get the currently logged on user 
# works okay for a straight python app/prog 
if sys.platform == 'win32': 
    userName = os.getenv('USERNAME') 
else: #linux but also not forgetting mac 
     userName = os.getenv('USER') 

# when code run in a python win service userName in None here!!!! 
# so lets get platform specific 
if not userName: 
    if sys.platform == 'win32': 
     import win32api 
     userName = win32api.GetUserName() 

# do you see 'SYSTEM' here - NOT the currently logged on user 
0

为了让您的服务继续运行,即使用户注销时需要指定的控制处理程序,它会一直开始之前返回True

if __name__ == "__main__": 
    win32api.SetConsoleCtrlHandler(lambda x: True, True) 
    win32serviceutil.HandleCommandLine(PythonService) 
相关问题