Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyinstaller Error starting service: The service did not respond to the start or control request in a timely fashion

I have been searching since a couple of days for a solution without success. We have a windows service build to copy some files from one location to another one.

So I build the code shown below with Python 3.7. The full coding can be found on Github.

When I run the service using python all is working fine, I can install the service and also start the service.

This using commands:

Install the service:

  • python jis53_backup.py install

Run the service:

  • python jis53_backup.py start

When I now compile this code using pyinstaller with command:

  • pyinstaller -F --hidden-import=win32timezone jis53_backup.py

After the exe is created, I can install the service but when trying to start the service I get the error:

Error starting service: The service did not respond to the start or control request in a timely fashion

I have gone through multiple posts on Stackoverflow and on Google related to this error however, without success. I don't have the option to install the python 3.7 programs on the PC's that would need to run this service. That's why we are trying to get a .exe build.

I have made sure to have the path updated according to the information that I found in the different questions.

Image of path definitions:

enter image description here

I also copied the pywintypes37.dll file.

From -> Python37\Lib\site-packages\pywin32_system32

To -> Python37\Lib\site-packages\win32

Does anyone have any other suggestions on how to get this working?

'''
    Windows service to copy a file from one location to another
    at a certain interval.
'''
import sys
import time
from distutils.dir_util import copy_tree

import servicemanager
import win32serviceutil

import win32service
from HelperModules.CheckFileExistance import check_folder_exists, create_folder
from HelperModules.ReadConfig import (check_config_file_exists,
                                      create_config_file, read_config_file)
from ServiceBaseClass.SMWinService import SMWinservice

sys.path += ['filecopy_service/ServiceBaseClass',
             'filecopy_service/HelperModules']


class Jis53Backup(SMWinservice):
    _svc_name_ = "Jis53Backup"
    _svc_display_name_ = "JIS53 backup copy"
    _svc_description_ = "Service to copy files from server to local drive"

    def start(self):
        self.conf = read_config_file()
        if not check_folder_exists(self.conf['dest']):
            create_folder(self.conf['dest'])

        self.isrunning = True

    def stop(self):
        self.isrunning = False

    def main(self):
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)
        while self.isrunning:
            # Copy the files from the server to a local folder
            # TODO: build function to trigger only when a file is changed.
            copy_tree(self.conf['origin'], self.conf['dest'], update=1)
            time.sleep(30)


if __name__ == '__main__':
    if sys.argv[1] == 'install':
        if not check_config_file_exists():
            create_config_file()

    if len(sys.argv) == 1:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(Jis53Backup)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(Jis53Backup)
like image 515
Peter Pallen Avatar asked Nov 06 '22 22:11

Peter Pallen


1 Answers

I was also facing this issue after compiling using pyinstaller. For me, the issue was that I was using the paths to configs and logs file in dynamic way, for ex:

curr_path = os.path.dirname(os.path.abspath(__file__)) 
configs_path = os.path.join(curr_path, 'configs', 'app_config.json')
opc_configs_path = os.path.join(curr_path, 'configs', 'opc.json')
log_file_path = os.path.join(curr_path, 'logs', 'application.log')

This was working fine when I was starting the service using python service.py install/start. But after compiling it using pyinstaller, it always gave me error of not starting in timely fashion.

To resolve this, I made all the dynamic paths to static, for ex:

configs_path = 'C:\\Program Files (x86)\\ScantechOPC\\configs\\app_config.json'
opc_configs_path = 'C:\\Program Files (x86)\\ScantechOPC\\configs\\opc.json'
debug_file = 'C:\\Program Files (x86)\\ScantechOPC\\logs\\application.log'

After compiling via pyinstaller, it is now working fine without any error. Looks like when we do dynamic path, it doesn't get the actual path to files and thus it gives error.

Hope this solves your problem too. Thanks

like image 137
S Andrew Avatar answered Nov 14 '22 22:11

S Andrew