私はこの質問を使用して、ActivePythonを使用したサービスとしてPythonスクリプトをインストールする方法を学びました。予想どおり、スクリプトをインストールして削除することはできますが、問題は、スクリプトの実行を開始すると、スクリプトを停止または削除することができなくなることです。それは永久に実行されるだけで、私はそれを取り除くことができません。リンクされた質問への回答は、スクリプトを停止するためのフラグのチェックに言及しています。誰かがこれを行う方法を説明できますか?
現在、スクリプトはほとんど何もしません。数秒ごとにファイルに行を出力するだけです。もっと複雑なことに移る前に、それを機能させたいと思っています。
import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import time
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "PythonTest"
_svc_display_name_ = "Python Test"
_svc_description_ = "This is a test of installing Python services"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def main(self):
i=0
while True:
f=open("C:\\hello.txt","a")
f.write("hello"+str(i)+"\n")
f.close()
i=i+1
time.sleep(5)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)