I have created a program in Python. Its task is to check some logs and it will perform some activity.
Reg_Version.py
class RegisterService:
.
.
def performAction(self):
self.__logFileSizeCheck()
self.__getHostName()
self.__deteleFiles()
self.__createFiles()
.
.
class Service(win32serviceutil.ServiceFramework):
_svc_name_ = '_test'
_svc_display_name_ = '_Service Template'
def __init__(self, *args):
win32serviceutil.ServiceFramework.__init__(self, *args)
self.log('init')
self.stop_event = win32event.CreateEvent(None, 0, 0, None)
def log(self, msg):
servicemanager.LogInfoMsg(str(msg))
def sleep(self, sec):
win32api.Sleep(sec*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)
# to be overridden
def start(self): pass
# to be overridden
def stop(self): pass
def instart(cls, name, display_name=None, stay_alive=True):
cls._svc_name_ = name
cls._svc_display_name_ = display_name or name
cls._svc_description_ = "Register service if it fails to register."
try:
module_path=modules[cls.__module__].__file__
except AttributeError:
# maybe py2exe went by
from sys import executable
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
)
print 'Install ok'
win32serviceutil.StartService(
cls._svc_name_
)
print 'Start ok'
except Exception, x:
print str(x)
class Test(Service):
def start(self):
self.serv = RegisterService() #<---RegisterService() is my created class
self.runflag=True
while self.runflag:
self.serv.performAction() #<---The method is called here
self.sleep(60)
self.log("Service is alive")
def stop(self):
self.runflag=False
self.log("Service is stopped")
instart(Test, 'Myservice', 'MyServerTest_1')
The script runs well when executed from cmd prompt
C:\Windows\system32>python "C:\Users\Arijit\Desktop\New folder (2)\Reg_Version.py"
Install ok
Start ok
Up to here everything works well.
Now I want it to create an executable windows service so that I can deploy the program to other systems. After googling I came to know that it can be done using pyinstaller / py2exe . I first used pyinstaller and it converted to a single executable package. After executing the .exe as admin, the service showed on the services.msc but it didn't start not even when clicking the start option. I check the eventvwr and found the following error
error 1053 the service did not respond to the start or control request in a timely fashion
I tried with py2exe, and getting the same issue. The service is getting install but not getting started.
I referred this link Python Windows service pyinstaller executables error 1053 but it didn't solve.