I have used configparser to read a configuration file in python. When the script is converted to Windows Service it is giving error saying "Error starting service: The service did not respond to the start or control request in a timely fashion."
I have increased ServicesPipeTimeout
and enabled Allow service to interact with desktop
but it did not solve the issue.
The script is
import logging
import configparser
import mysql.connector
import socket
import win32event
import win32service
import servicemanager
import win32serviceutil
import sys
logging.basicConfig(
filename = "C:\\Users\\Administrator\\PythonService.txt",
level = logging.INFO,
format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p'
)
def main():
configaration = configparser.ConfigParser()
configaration.read('default.ini')
host = configaration['mysql']['host']
username = configaration['mysql']['user']
password = configaration['mysql']['password']
database = configaration['mysql']['database']
config = {'user': username,'password': password,'host': host,'database': database}
logging.info(config)
class TestService(win32serviceutil.ServiceFramework):
_svc_name_ = "ClientService"
_svc_display_name_ = "Client Service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
self.stop_requested = False
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
logging.info('Stopping service ....')
self.stop_requested = True
def SvcDoRun(self):
logging.info("Started service")
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
rc = None
while rc != win32event.WAIT_OBJECT_0:
rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)
if rc == win32event.WAIT_OBJECT_0:
servicemanager.LogInfoMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
break
else:
main()
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(TestService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(TestService)
The execution stops when script starts interacting with the file in the filesystem.