0

I have been attempting to run my python script as a service, and have followed the advice contained in several previous forum posts. However, these have not helped me thus far. Here is what I have attempted up until now:

  • Used an SMWinservice class that allowed me to install my Python script as a service. This solution, however, would not launch. If I would try to start it, I would receive an error message.

  • I have tried using NSSM with : nssm install myService pathToInterpreter PathToScript. Note, all files etc. can be found in the local directory of the PathToScript. For what its worth, I also tried using the GUI version of NSSM. When I rebooted my computer, it showed as "Paused" in task manager. I stopped it, and then tried to run it again, and I received an error. I have tried NSSM with both python.exe and pythonw.exe.

I suspect that a possible source of error is the fact that my program uses a text file as a configuration file. This configuration file has been coded as being in my local working directory. However, I wouldn't think this would be an issue with NSSM. I know for a fact that my program will completely exit, using the exit command, if the configuration file is not found.

I was thinking of doing a batch file and starting the batch file with the script and running it like that, but I prefer a service since it can monitor the process, can restart it, or windows can notify me via email if there is an issue with my service.

For completeness, I should also mention that the program runs without issue outside of a service. For the reason that the program runs as expected, I decided to not post the code, unless someone would like to see it for whatever reason.

martineau
  • 119,623
  • 25
  • 170
  • 301
GeekGeek4
  • 149
  • 9
  • Maybe write to log some info to see which part of code is executed. You can also check if system runs code in folder with config - it may run it in different folder and then all files should use full path. – furas Sep 10 '19 at 00:30
  • I included write to file calls throughout different parts of the program that would essentially mark checkpoints in the execution of the program. However, after rebooting, and therefore the program executing as a service, I was unable to locate an instance of the log file, leading me to believe that the program is not executing in the first place. I will look further into why nothing is happening. Thank you for your suggestion. – GeekGeek4 Sep 10 '19 at 15:18
  • I am currently launching the program as a service, just by launching the Main.py. This script simply contains an infinite while loop, and uses the code of other classes. There is nothing special about the script and does not include/implement any special classes related to services. Is this incorrect? – GeekGeek4 Sep 10 '19 at 15:28
  • I know GUIs interfere with services, which is why I explicitly made use of pythonw.exe – GeekGeek4 Sep 10 '19 at 15:30
  • This is the error that I am seeing if I try to resume my python service, which as I mentioned above, is paused when I start the computer. Error when "resuming": Windows could not resume the MyService service on Local Computer. The service did not return an error. This could be an internal Windows error or an internal service error. If the problem persists, contact your system administrator. – GeekGeek4 Sep 10 '19 at 15:44
  • Another note: I am using python 3.7. Many solutions seem to work for previous versions of python – GeekGeek4 Sep 10 '19 at 17:59
  • I don't use WIndows to check it but if it works with previous versions then install one of them. On Linux I have installed 3.7, 3.6, 3.5 and even 2.7 because some modules don't work with newer version. I would also use rather `python` instead of `pythonw`. Service shouldn't use any GUI and works only in terminal/console. As I know services may run as different user, with different privillege, in different fodler so it is good to use full pathes for files and write in folder which has privilleges for all users. – furas Sep 10 '19 at 18:37

0 Answers0