简体   繁体   中英

Python program crashes when run through a Windows service

My client has provided me with a Python console application which performs some work and writes the result into a .txt file. My task is to write a Windows service which reads that particular .txt file and performs further actions.

I used C# on .NET to write the service. My solution contains 3 projects:

  • The logic layer project.
  • The Windows service layer project.
  • A test app layer project, used for debugging and other purposes.

Both the Windows service layer and test app layer are using the logic layer for core functionality. When I run the application through the test layer, everything works perfectly, but whenever I try to run the application through the service, the Python standalone application that the service launches doesn't write any output files. I could see that the Python app runs in the task manager, but there's no output anywhere. I believe the Python code is crashing but I couldn't get the exact reason.

I've tried the following ways to debug the issue:

  • Searched the Windows and System32 directories for any related output files, just to consider the possibility of the service having these directories as the default working directory.
  • Used absolute paths in the service code to make sure that the Python part is not writing output files to some unknown location.
  • Had the client implement passing the output directory to the Python code through command line arguments.
  • Wrote a mock console app in C# which writes a file, tried to call it through the service, but it worked fine and wrote the file as expected.
  • Suspected the standard IO could be causing the Python application to crash and thus used the standard IO in my mock program, but it worked without any issues.
  • Tried giving a long task to the Python code, which should've taken about 30 minutes to execute completely, but the Python script ran and closed immediately, which essentially is reliable proof of the theory that it crashes at some point.
  • Tried running the service with my unelevated Windows user instead of the Local System pseudouser.
  • Tried configuring the service to be able to interact with the desktop.

I am all out of ideas here. Any direction I should also search in?

Just a note, if that matters: I am using System.Diagnostics.Process to launch the Python script.

If it works from your test app, it sounds like a permissions issue. What security context / user is the windows service running as, and does that user have permission to write to the filesystem where you are expecting it? Have you tried using a full path to the output file to be sure where it is expected?

I'd be inclined to write a tiny python app that just saves "hello world" to a file, and get that to work from a windows service, then build up from there.

Thanks to the help from timhowarduk, I finally was able to get to the root cause of the problem. The python script was looking for a configuration file, and when it was running from the Windows Service, it was looking for that config file in System32.

All the windows services are run from System32.

The above caused the python script to search in System32 since it was running as part of the windows service. I guess I might just ask the client to edit the python script to read config from the windows service application directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM