Python Windows service pyinstaller executables error 1053 -
i have written windows service in python. if run script command prompt
python runservice.py
when service installs , starts correctly. have been trying create executable using pyinstaller because i've seen same issue py2exe. when run .exe service installs not start , following error
error 1053 service did not respond start or control request in timely fashion
i have seen many people have had issue can't seem find definitive answer how fix this.
winservice.py
from os.path import splitext, abspath sys import modules, executable time import * import win32serviceutil import win32service import win32event import win32api class service(win32serviceutil.serviceframework): _svc_name_ = '_unnamed' _svc_display_name_ = '_service template' _svc_description_ = '_description template' def __init__(self, *args): win32serviceutil.serviceframework.__init__(self, *args) self.log('init') self.stop_event = win32event.createevent(none, 0, 0, none) #logs system event log def log(self, msg): import servicemanager servicemanager.loginfomsg(str(msg)) def sleep(self, minute): win32api.sleep((minute*1000), true) def svcdorun(self): self.reportservicestatus(win32service.service_start_pending) try: self.reportservicestatus(win32service.service_running) self.log('start') self.start() self.log('wait') win32event.waitforsingleobject(self.stop_event, win32event.infinite) self.log('done') except exception, x: self.log('exception : %s' % x) self.svcstop() def svcstop(self): self.reportservicestatus(win32service.service_stop_pending) #self.log('stopping') self.stop() #self.log('stopped') win32event.setevent(self.stop_event) self.reportservicestatus(win32service.service_stopped) # overridden def start(self): pass # overridden def stop(self): pass def instart(cls, name, description, display_name=none, stay_alive=true): ''' install , start (auto) service cls : class (derived service) implement service name : service name display_name : name displayed in service manager decription: description stay_alive : service stop on logout if false ''' cls._svc_name_ = name cls._svc_display_name_ = display_name or name cls._svc_desciption_ = description try: module_path=modules[cls.__module__].__file__ except attributeerror: module_path=executable module_file = splitext(abspath(module_path))[0] cls._svc_reg_class_ = '%s.%s' % (module_file, cls.__name__) if stay_alive: win32api.setconsolectrlhandler(lambda x: true, true) try: win32serviceutil.installservice( cls._svc_reg_class_, cls._svc_name_, cls._svc_display_name_, starttype = win32service.service_auto_start, description = cls._svc_desciption_ ) print 'install ok' win32serviceutil.startservice( cls._svc_name_ ) print 'start ok' except exception, x: print str(x)
update
i resolved issue using py2exe same changes may work pyinstaller too. haven't had time check out myself.
i had remove instart function. below how winservice.py reads now.
winservice_py2exe.py
from os.path import splitext, abspath sys import modules, executable time import * import win32serviceutil import win32service import win32event import win32api class service(win32serviceutil.serviceframework): _svc_name_ = 'actualservicename' #here name input arg instart _svc_display_name_ = 'actualdisplayname' #arg instart _svc_description_ = 'actualdescription'# arg instart def __init__(self, *args): win32serviceutil.serviceframework.__init__(self, *args) self.log('init') self.stop_event = win32event.createevent(none, 0, 0, none) #logs system event log def log(self, msg): import servicemanager servicemanager.loginfomsg(str(msg)) def sleep(self, minute): win32api.sleep((minute*1000), true) def svcdorun(self): self.reportservicestatus(win32service.service_start_pending) try: self.reportservicestatus(win32service.service_running) self.log('start') self.start() self.log('wait') win32event.waitforsingleobject(self.stop_event, win32event.infinite) self.log('done') except exception, x: self.log('exception : %s' % x) self.svcstop() def svcstop(self): self.reportservicestatus(win32service.service_stop_pending) #self.log('stopping') self.stop() #self.log('stopped') win32event.setevent(self.stop_event) self.reportservicestatus(win32service.service_stopped) # overridden def start(self): pass # overridden def stop(self): pass if __name__ == '__main__': # note code not run in 'frozen' exe-file!!! win32serviceutil.handlecommandline(vidiagservice) #added example included py2exe
below setup.py file used py2exe. taken example included py2exe installation:
setup.py
from distutils.core import setup import py2exe import sys if len(sys.argv) == 1: sys.argv.append("py2exe") sys.argv.append("-q") class target: def __init__(self, **kw): self.__dict__.update(kw) # versioninfo resources self.version = "0.5.0" self.company_name = "no company" self.copyright = "no copyright" self.name = "py2exe sample files" myservice = target( # used versioninfo resource description = "a sample windows nt service", # build. service, module name (not # filename) must specified! modules = ["winservice_py2exe"] ) setup( options = {"py2exe": {"typelibs": # typelib wmi [('{565783c6-cb41-11d1-8b02-00600806d9b6}', 0, 1, 2)], # create compressed zip archive "compressed": 1, "optimize": 2}}, # lib directory contains except executables , python dll. # can include subdirectory name. zipfile = "lib/shared.zip", service = [myservice] )
once create exe can install service command using following command
winservice_py2exe.exe -install
then start service can use:
net start atest
or windows service manager. other windows command line functionality works on service windows service manager.
try changing last few lines to
if __name__ == '__main__': if len(sys.argv) == 1: servicemanager.initialize() servicemanager.preparetohostsingle(service) servicemanager.startservicectrldispatcher() else: win32serviceutil.handlecommandline(service)
Comments
Post a Comment