用Python编写的Windows服务无法启动

时间:2019-01-16 14:45:17

标签: python windows service

我无法启动我的Python服务。安装后,当我尝试运行它时,进度栏会变一点,然后出现以下错误:

  

Windows无法在本地计算机中启动服务“ X” ...请与服务供应商联系,并参考特定于服务的错误代码1。

当我尝试调试服务时,无法获得任何有用的信息:

  

python SIMailService.py debug

     

调试服务SIMailService-按Ctrl + C停止。   错误0xC000000A-Python无法构造类实例

     

     

(空):(空)

当我尝试从控制台启动服务时,它只是停止而没有任何消息:

  

> python SIMailService.py start

     

启动服务SIMailService

     

C:\Users\Kamil\PycharmProjects\SIMail\src>

我已经尝试将Python的路径添加到SYSTEM PATH,如此处所述:https://stackoverflow.com/a/14580685/2550466。这没有结果。

from Application import Application
from Entities import *
from InternetHost import *
import socket
import sys

import win32serviceutil

import servicemanager
import win32event
import win32service

import schedule
import time


class SMWinservice(win32serviceutil.ServiceFramework):
    '''Base class to create winservice in Python'''

    _svc_name_ = 'pythonService'
    _svc_display_name_ = 'Python Service'
    _svc_description_ = 'Python Service Description'

    @classmethod
    def parse_command_line(cls):
        '''
        ClassMethod to parse the command line
        '''
        win32serviceutil.HandleCommandLine(cls)

    def __init__(self, args):
        '''
        Constructor of the winservice
        '''
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        socket.setdefaulttimeout(60)

    def SvcStop(self):
        '''
        Called when the service is asked to stop
        '''
        self.stop()
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        '''
        Called when the service is asked to start
        '''
        self.start()
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_, ''))
        self.main()

    def start(self):
        '''
        Override to add logic before the start
        eg. running condition
        '''
        pass

    def stop(self):
        '''
        Override to add logic before the stop
        eg. invalidating running condition
        '''
        pass

    def main(self):
        '''
        Main class to be ovverridden to add logic
        '''
        pass

# This class contains main utility for the service
class AppService(Application):

    def __init__(self, consoleLog=False):

        super().__init__('SIMail Service')


        self.handlerFile = logging.FileHandler('service.log')
        self.handlerFile.setLevel(logging.DEBUG)
        self.handlerFile.setFormatter(self.formatterMain)
        self.loggerMain.addHandler(self.handlerFile)

        if consoleLog is True:
            self.handlerConsole = logging.StreamHandler()
            self.handlerConsole.setLevel(logging.DEBUG)
            self.handlerConsole.setFormatter(self.formatterMain)
            self.loggerMain.addHandler(self.handlerConsole)

        self.regInDb = True

# This is the main service class
class SIMailService(SMWinservice):
    _svc_name_ = "SIMailService"
    _svc_display_name_ = "SIMailService"
    _svc_description_ = "Usługa analizy stanów kont routerów"

    def __init__(self):
        super().__init__()
        self._appService = AppService(False)

    def start(self):
        self.isrunning = True

    def stop(self):
        self.isrunning = False

    def refreshRoutersBalance(self):
        self._appService.loggerMain.info('Rozpoczynam odpytywanie')
        self._appService.gather('all')


    def main(self):
        timer = schedule.every(15).minutes
        timer.do(self.refreshRoutersBalance)
        while self.isrunning:
            schedule.run_pending()
            time.sleep(1)


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

您是否还有其他想法,此时此刻可能出问题?

0 个答案:

没有答案