-
-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Windows Service cannot find required configuration files after creating .exe using cx_Freeze #2440
Comments
Here is a example service that starts and stops. |
Use this information to test: https://github.com/marcelotduarte/cx_Freeze/tree/main/samples/service#run-the-sample Run in a command prompt or powershell with admin privileges. MainSoftware --install test I have an automated test, and tested it now, and it it ok. I recommend using Python 3.10+ because previous versions have bugs with Python. |
Here is my execution on a shell with admin privileges: C:\Users\sk\Desktop\testservice\build\agent>MainCode --install test The output of the log file is as: [20452] 2024/06/07 14:14:41.300 starting logging at level ERROR [20452] 2024/06/07 14:14:41.835 File "C:\Users\sk\anaconda3\envs\dq\Lib\site-packages\cx_Freeze\initscripts_startup_.py", line 141, in run [20452] 2024/06/07 14:14:41.836 File "C:\Users\sk\anaconda3\envs\dq\Lib\site-packages\cx_Freeze\initscripts\console.py", line 25, in run [20452] 2024/06/07 14:14:41.836 File "MainCode.py", line 503, in [20452] 2024/06/07 14:14:41.836 SystemExit [20452] 2024/06/07 14:14:41.837 ending logging The persistant issue remains that the service cannot access the configuration files needed to start MainCode which exist in a file titled conf in the directory. I have attempted to amend the test line in shell as such: MainCode C:\Users\sk\Desktop\testservice\build\agent --install test Such that the configuration files exist in the directory C:\Users\sk\Desktop\testservice\build\agent\conf |
I think this message says it all. Check that these files are being copied to the correct folder, and adjust your include_files accordingly. |
Sorry, I think I understand your problem. Look this: Edit: This information is also relevant: https://cx-freeze.readthedocs.io/en/stable/faq.html#using-data-files |
I have attempted to resolve the issue using Issue #885 and did not have much success. See below for my updated ServiceHandler.py from future import annotations See below for more information on what methods must be implemented and how they class Handler:
Running MainCode --install test yields the following: Directory provided: --install And the service creation log file shows the following: [15116] 2024/06/10 13:29:12.686 File "C:\Users\sk\anaconda3\envs\dqagent\Lib\site-packages\cx_Freeze\initscripts_startup_.py", line 141, in run [15116] 2024/06/10 13:29:12.686 File "C:\Users\sk\anaconda3\envs\dq\Lib\site-packages\cx_Freeze\initscripts\console.py", line 25, in run [15116] 2024/06/10 13:29:12.686 File "MainCode.py", line 573, in [15116] 2024/06/10 13:29:12.686 File "C:\Users\sk\anaconda3\envs\dq\Lib\configparser.py", line 979, in getitem [15116] 2024/06/10 13:29:12.686 KeyError: 'NAME' Line 573 in my script is the following line of code: log_filename = (logging_config['NAME']['agent_logfile']+".log") Where the log_filename variable is being created using the filename provided in the config file. The issue remains that the configuration files cannot be accessed and read by the service (the script uses the configparser library to read the config files), despite the following line of code to ServiceHandler.py in the initialize function(): self.directory = os.path.dirname( os.path.abspath( os.getcwd() + "_file_")) I also attempted to create the service using the following shell command, and start the service manually from Services, with the same result: Is there another way for me to ensure that the service knows/can access the configuration files? The service .exe is in the path C:\Users\sk\Desktop\testservice\agent\build\agent\MainCode.exe and the conf folder holding the configuration files are in C:\Users\sk\Desktop\testservice\agent\build\agent\conf |
Does not work, check the value of getcwd...
Following my advice, you should have tried: |
I tried that approach, which didn't work on my end at which point I tried: self.directory = os.path.dirname( os.path.abspath( os.getcwd() + "file")) Here are the results with: self.directory = os.path.dirname(sys.executable) Here is my ServiceHandler.py See below for more information on what methods must be implemented and how they class Handler:
After creating the .exe attempting to run MainCode --install test yields the following console result: And the logfile reads the following, indicating that the configuration files could not be found: [24436] 2024/06/10 14:42:55.250 File "C:\Users\sk\anaconda3\envs\dq\Lib\site-packages\cx_Freeze\initscripts_startup_.py", line 141, in run [24436] 2024/06/10 14:42:55.253 File "C:\Users\sk\anaconda3\envs\dq\Lib\site-packages\cx_Freeze\initscripts\console.py", line 25, in run [24436] 2024/06/10 14:42:55.253 File "MainCode.py", line 507, in [24436] 2024/06/10 14:42:55.253 SystemExit [24436] 2024/06/10 14:42:55.254 ending logging Attempting to run the service with another shell command shown before (sc create MainCode start=delayed-auto binPath= "C:\Users\sk\Desktop\testservice\agent\build\agent\MainCode.exe C:\Users\sk\Desktop\testservice\agent\build\agent" then manually starting the service in Services) yields the same result. |
I'll try to explain to you what I think it is another way. When you start a common program, the current folder (getcwd) is the starting directory, but in "service" the current directory is not the current folder or the installation folder. It's probably C:\windows\system32. So, please try it:
|
Hi,
I am attempt to create a few executables that will be run as a Windows Service. Here is my code for creating the executables:
This creates the executables correctly. However, when I attempt to initialize MainSoftware.exe as a windows service using the following command:
sc create MainSoftware start= delayed-auto binPath= "C:\work\build\tool\MainSoftware.exe C:\work\build\tool"
The windows service is created but it cannot start due to an error "error 1503 the service didn't respond in a timely fashion". Checking the log file, it seems to be because the service cannot access the configuration files required for my code to run (devicePrefs.conf, appVersion.conf, and loggingPrefs.conf located in the conf folder - see "include_files" declaration above).
How can I amend this issue, where the service is able to pick up these config files? Here is how my ServiceHandler.py script is set up:
Thanks in advance!
The text was updated successfully, but these errors were encountered: