2017-07-03 6 views
0

私はすでにPythonスクリプトを継続的に実行しています。 https://github.com/walchko/Black-Hat-Python/blob/master/BHP-Code/Chapter10/file_monitor.pyPythonスクリプトをWindowsサービスに変換する際の問題

同様に、スクリプトとして実行すると、何かが起きたときに何らかのデータを表示するCMDが開きます。これはユーザーとのやりとりができないため、表示する必要はありません誰かがWindowsサービスがインターフェイスを持つことができないことを指摘したい)

私はそれをサービスに変換しようとしました。それは数秒間始まり、自動的に停止します。 services.msc(python script.py startの代わりに)を使って起動しようとすると、それはまったく起動しません.Windows errorには「ローカルコンピュータのサービスが起動して停止しました」というメッセージが表示されます。私は議論を開始しようとします。

私はそれをサービスとして実行できるようにスクリプトを変更しようとした

- 私はここで見つける骨格を追加:Is it possible to run a Python script as a service in Windows? If possible, how?

私もちょうど上のスケルトンスクリプトを取得し、それを実行しようとしてみました他のスクリプトはここからの例です:What is the best way to call a Python script from another Python script?

上記のスクリプトをサービスとして実行するにはどのような行動を取るのがよいでしょうか?

ありがとうございます!

答えて

0

編集

「...コンピュータの起動は、 が一時停止して再起動し、任意のユーザーインターフェイスを表示しないことができたときにサービスが自動的に起動することができます。」Introduction to Windows Service Applications

Windowsサービスが特定のインターフェイスを利用できるように実装が必要になります。

ですから、Pythonの経由のWindows APIにアクセスする必要があります。

あなたはPython Programming On Win32から、example codeを見ることができ、内の第18章サービス(ch18_servicesフォルダ)は、試料(SmallestServiceが含まれています。

# SmallestService.py 
# 
# A sample demonstrating the smallest possible service written in Python. 

import win32serviceutil 
import win32service 
import win32event 

class SmallestPythonService(win32serviceutil.ServiceFramework): 
    _svc_name_ = "SmallestPythonService" 
    _svc_display_name_ = "The smallest possible Python Service" 
    def __init__(self, args): 
     win32serviceutil.ServiceFramework.__init__(self, args) 
     # Create an event which we will use to wait on. 
     # The "service stop" request will set this event. 
     self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) 

    def SvcStop(self): 
     # Before we do anything, tell the SCM we are starting the stop process. 
     self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
     # And set my event. 
     win32event.SetEvent(self.hWaitStop) 

    def SvcDoRun(self): 
     # We do nothing other than wait to be stopped! 
     win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) 

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

あなたがここにあなたの特定のPython環境のために適切なpywin32ホイールをダウンロードする必要があります: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32

そしてinstallそれを(システムPY)Pythonで書か可能な最小のサービスを実証admin cmdプロンプトから):

> cd \program files\python<ver>\scripts 
> pip install \path\to\pywin32‑221‑cp<ver>‑cp<ver>m‑win_<arch>.whl 

または(ユーザーあたり、通常のCMDプロンプトから)それをインストール:

> cd \program files\python<ver>\scripts 
> pip install --user \path\to\pywin32‑221‑cp<ver>‑cp<ver>m‑win_<arch>.whl 

は適切<ver><arch>の出現箇所を置き換えてください。

+0

ありがとうございました!そして、もし私のサービスが他のスクリプトを実行させたいのであれば、どこでどのようにコードを書かなければなりませんか?私はちょうどwin32serviceutil.HandleCommandLine(SmallestPythonService)の下にsubprocess.call( "script.py")のようなものを置くべきですか? –

+0

SvcDoRun()関数の内部からスクリプトを起動してみてください。 – veganaiZe

+0

私はあなたの答えをそれがうまくいくように受け入れました。サービスがインストールされていて、同じCMDでスクリプトが実行され始めました。私は "python script.py install"コマンドを使用しました。しかし、それは開始されていない、私はそのCMDウィンドウを開いたままにしている限り動作しています。私はこれを取得しています: "インスタンスのSvcRun()メソッドが失敗しました Windowsイベントビューアで<トレースバック取得中にtraceback.print_exception()が失敗しました %2:%3" –

関連する問題