Python NSSM commands packaged in .exe

30 Views Asked by At

I have an app that I built with customtkinter. It needs to configure a file and install a python script as a service. That service reads from the config file. It works when I run it from pycharm but when I use auto_py_to_exe the service fails to start with error code 3.

my code runs the following command to install the service and works from pycharm:

cmds = [
            'C:/RT_Apps/RT_SIP_Sounder/config/nssm install "RT SIP Alert" "C:/Users/crixe/PycharmProjects/intercomSounder/venv/Scripts/python.exe" "C:/RT_Apps/RT_SIP_Sounder/svc_build/intercom.py"'
        ]
        for cmd in cmds:
            subprocess.run(cmd, cwd="C:/RT_Apps/RT_SIP_Sounder/config", stdout=subprocess.PIPE)
            print(f"CMD RUN: {cmd} ")
            time.sleep(3)

I have tried to point to a local python.exe file included with the exe build, even including the Scripts folder. I know I'm doing something real dumb, but can't seem to figure out what it is.

1

There are 1 best solutions below

0
cmrussell On

Don't Know if this will help anybody else.

If you use the NSSM GUI you have three options:

  1. Path
  2. Startup directory
  3. Arguments

Path points to your executable. If you're running a python script as a service though, the Path should be set to wherever python.exe is.

The full path to your python script you want to run as a service should be split between the Startup directory and the arguments.

So if the script is in C:/myAppFolder/service_dir/service.py,then you would (or could) set the Startup directory to c:/myAppFolder/ (watch the slash) with the arguments being service_dir/service.py

enter image description here

If you were installing from a script you would run the install followed by the set commands:

nssm install "My Service Name"
nssm set "My Service Name" Application "C:/myAppFolder/python/python.exe"
nssm set "My Service Name" AppDirectory "C:/myAppFolder/"
nssm set "My Service Name" AppParameters "service_dir/service.py"

This is what has worked for me in getting NSSM to install a py script as windows service without pausing.