A proof-of-concept on how to run a python script as a windows service using nssm.
NSSM (the Non-Sucking Service Manager) allows easy creation of windows services with a few simple commands:
nssm install "SERVICE_NAME" "PATH_TO_PYTHON.exe" "PATH_TO_PYTHON_SCRIPT.py"
- Configure logging (optional)
nssm set SERVICE_NAME Appstdout "PATH_TO_STDOUT.log"
(file must exist)nssm set SERVICE_NAME Appstderr "PATH_TO_STDERR.log"
(file must exist)
nssm start SERVICE_NAME
For more information see the link here or under the Acknowledgements section. Also have a look at /scripts
for example usage.
- nssm downloaded and added to your
PATH
- A python environment with the packages from
requirements.txt
installed - The location of
python.exe
of said environment (typically/venv/Scripts/python.exe
) - (optional) create files for logging, nssm does not do this by itself.
The /scripts
folder contains examples on how to "install" the service. You must probably modify the paths according to your system.
If successfully installed you should see "Hello, World!" if you go to http://192.168.0.217:9000/. IP address might differ, see logs.
- Download and extract nssm to a location of your choice.
- Add the root folder of the nssm.exe file to your PATH variable, i.e.
C:\nssm-2.24\nssm-2.24\win64
Thanks to https://medium.com/@m_ko/how-to-run-python-scripts-as-a-windows-service-979082706360