2016-04-07 8 views
2

私はStackOverflow上でこれと似た話題があることを知っていますが、それらのどれも私と同じ問題はありません。ほとんどの質問は、Pythonからサービスを開始する方法を尋ねています。私はサービスを作成している.batファイルを持っていて、py2exeを使って作成したPythonFile.exeを使っています。エラー "サービスの開始中にエラーが発生しました。サービスが開始または制御要求に適時に応答しませんでした"。サービスは実行されませんが、ProcessManagerプロセスで実行可能ファイルが表示されます。PythonがWindowsサービスとして実行可能

実行ファイルがサービスとして適格になるための具体的な要件はありますか?私の実行可能ファイルは、mutexのロックが解除されるまで(mutexを使用して)スリープするTCPサーバーです。

マイ.batファイル...

net stop "FabulousAndOutrageousOinkers" 
%SYSTEMROOT%\system32\sc.exe delete "FabulousAndOutrageousOinkers" 
%SYSTEMROOT%\system32\sc.exe create "FabulousAndOutrageousOinkers" binPath= "%CD%\FabulousAndOutrageousOinkers.exe" start= auto 
net start "FabulousAndOutrageousOinkers" 

答えて

3

私は私の質問への答えを見つけてしまいました。実際にはサービスになるという要件があります。サービスになるほとんどのスクリプトやプログラムには、これらの要件の処理を管理するコードの上にラッパー層があります。このラッパーは、開発者のコ​​ードを呼び出し、さまざまな種類のステータスでWindowsサービスに信号を送ります。開始、停止など...

import win32service 
import win32serviceutil 
import win32event 

class Service(win32serviceutil.ServiceFramework): 
    # you can NET START/STOP the service by the following name 
    _svc_name_ = "FabulousAndOutrageousOinkers" 
    # this text shows up as the service name in the Service 
    # Control Manager (SCM) 
    _svc_display_name_ = "Fabulous And Outrageous Oinkers" 
    # this text shows up as the description in the SCM 
    _svc_description_ = "Truly truly outrageous" 

    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) 

    # core logic of the service  
    def SvcDoRun(self): 
     import servicemanager 
     self.ReportServiceStatus(win32service.SERVICE_START_PENDING) 

     self.start() 
     rc = None 

     # if the stop event hasn't been fired keep looping 
     while rc != win32event.WAIT_OBJECT_0: 
      # block for 5 seconds and listen for a stop event 
      rc = win32event.WaitForSingleObject(self.hWaitStop, 5000) 

     self.stop() 

    # 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) 

    def start(self): 
     try: 
      file_path = "FabulousAndOutrageousOinkers.exe" 
      execfile(file_path)    #Execute the script 
     except: 
      pass 

    def stop(self): 
     pass 

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

このテンプレートは、http://www.chrisumbel.com/article/windows_services_in_pythonから見つかりました。このコードには、「サービスを開始中にエラーが発生しました:サービスが開始または制御要求に適時に応答しませんでした」というエラーが表示されるため、まだいくつかの問題があります。実際には、実行可能ファイルがWindowsサービスになるための要件が​​あります。

関連する問題