0
我要運行我的python腳本是Win32服務如下啓動Win32服務:如何通過Python
aservice.py
import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os
class aservice(win32serviceutil.ServiceFramework):
_svc_name_ = "aservice"
_svc_display_name_ = "a service - it does nothing"
_svc_description_ = "Tests Python service framework by receiving and echoing messages over a named pipe"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
import servicemanager
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
self.timeout = 3000
from cron.realtimemonitorschedule import startMonitor
startMonitor()
while 1:
# Wait for service stop signal, if I timeout, loop again
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
# Check to see if self.hWaitStop happened
if rc == win32event.WAIT_OBJECT_0:
# Stop signal encountered
servicemanager.LogInfoMsg("aservice - STOPPED")
break
else:
servicemanager.LogInfoMsg("aservice - is alive and well")
def ctrlHandler(ctrlType):
return True
if __name__ == '__main__':
win32api.SetConsoleCtrlHandler(ctrlHandler, True)
win32serviceutil.HandleCommandLine(aservice)
我可以成功安裝服務,但是當我啓動服務,如下就會提示錯誤:
from cron.realtimemonitorschedule import startMonitor
ImportError: No module named cron.realtimemonitorschedule
注意cron.realtimemonitorschedule在realtimemonitorschedule.py定義,我的問題是,我怎麼可能把我的腳本從Win32服務?我無法將腳本嵌入上面的aservice.py
其中cron.realtimemonitorschedule安裝在您的環境中? – ciphor
@ ciphor,被羞辱,我是Python的新手。我不知道如何在ENV中安裝cron.realtimemonitorschedule,以便win32服務可以找到它:( –
因此,您可以在命令行中運行python腳本嗎? – ciphor