【问题标题】:Python :Unable to start installed windows servicePython:无法启动已安装的 Windows 服务
【发布时间】:2021-10-08 05:48:39
【问题描述】:

我在 python 中创建了一个 windows 服务,然后将其转换为一个 exe 文件,并使用此命令将该 exe 文件安装为 windows 服务:

sc create PC-Service binpath= "C:\\Users\\Slazenger\\Desktop\\auto-py-to-exe-master\\output\\testing.exe" DisplayName= "PC-Service" start=auto 

我收到了成功消息

[SC] CreateService SUCCESS 

但是当我尝试使用此命令安装服务时:

sc start PC-Service

我收到此错误消息

[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion.

我不知道为什么我会得到这个,因为当我 运行我的 python 脚本作为服务时它运行完美,但是当我尝试将 exe 文件作为服务运行时出现错误,请帮助我。 这是我的python脚本:

def WriteToFile():

    while True:
        file = open ( "C:\\file.txt" , "w" )
        now = datetime.now()
        now = now.strftime("%B %d, %y %H:%M:%S")
        file.write(now)

class Pythonservice(win32serviceutil.ServiceFramework):


    _svc_name_ = 'PC-Service'
    _svc_display_name_ = 'PC-Service'
    _svc_description_ = 'Freindly Service'

    @classmethod
    def parse_command_line(cls):

        win32serviceutil.HandleCommandLine(cls)

    def __init__(self, args):

        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        socket.setdefaulttimeout(60)

    def SvcStop(self):

        self.stop()
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):

        self.start()
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_, ''))
        self.main()

    def start(self):
        self.isrunning = True

    def stop(self):
       self.isrunning = False

    def main(self):
        WriteToFile()



if __name__ == '__main__':
    Pythonservice.parse_command_line()

请帮我摆脱这个谢谢。

【问题讨论】:

    标签: python windows-services


    【解决方案1】:

    我已经对您的代码进行了一些更改,希望之后它会开始工作。

    class MyService:
        def stop ( self ) :
            
            self.running = False
    
        def run ( self ) :
            
            self.running = True
            while self.running :
                WriteToFile()
    class MyServiceFramework(win32serviceutil.ServiceFramework):
        _svc_name_ = 'PC-Service'
        _svc_display_name_ = 'PC-Service '
    
        def SvcStop ( self ) :
            
            self.ReportServiceStatus ( win32service.SERVICE_STOP_PENDING )
            self.service_impl.stop ( )
            self.ReportServiceStatus ( win32service.SERVICE_STOPPED )
    
        def SvcDoRun ( self ) :
            
            self.ReportServiceStatus ( win32service.SERVICE_START_PENDING )
            self.service_impl = MyService ( )
            self.ReportServiceStatus ( win32service.SERVICE_RUNNING )
            
            self.service_impl.run ( )
    
    
    
    def init():
        if len ( sys.argv ) == 1 :
            servicemanager.Initialize ( )
            servicemanager.PrepareToHostSingle ( MyServiceFramework )
            servicemanager.StartServiceCtrlDispatcher ( )
        else :
            win32serviceutil.HandleCommandLine ( MyServiceFramework )
    
    if __name__ == '__main__':
        init()
    

    现在只需使用 pyinstaller 将其转换为 exe 文件。

    pyinstaller --noconfirm --onefile --windowed --hidden-import win32timezone path-to-py file
    

    然后将您的 exe 文件安装为服务,因此请转到 exe 文件所在的目录,通常它位于 dist 文件夹中。

    yourexefile.exe --startup delayed install
    

    您可以使用以下方式启动它:

    yourexefile.exe start
    

    希望这对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多