avatarmatteo

Summary

The article provides a tutorial on how to run Python scripts as a Windows service using the NSSM tool.

Abstract

The web content is a guide that instructs readers on setting up Python applications to run as Windows services. It begins with an introduction to what Windows services are and their benefits, such as running applications continuously and starting them automatically upon system boot. The author, aware of potential skepticism, offers a concise explanation of Windows services and their utility. The tutorial covers the necessary setup, which includes downloading the NSSM tool, installing the service with the correct Python and script paths, and configuring log files for standard output and error output. The article emphasizes the importance of creating log files and ensuring the existence of specified paths. It also provides command-line instructions for managing the service and suggests using the Windows Service GUI for troubleshooting. The guide concludes by encouraging readers to test the service and offers additional commands for stopping, restarting, and removing the service.

Opinions

  • The author acknowledges that some readers may question the need to run Python scripts as Windows services, indicating a potential gap in understanding among the audience.
  • The preference for simplicity and efficiency is evident as the author suggests using NSSM and provides a command to easily find the Python executable path.
  • The author advocates for the use of NSSM to create Windows services for Python scripts, implying that this method is superior to other possible solutions.
  • There is an emphasis on the importance of proper configuration, particularly for logging, to ensure the service runs smoothly and issues can be diagnosed effectively.
  • The author assumes that the reader may be somewhat familiar with command-line operations, as indicated by the use of terms like "admin" and the inclusion of command-line instructions without extensive explanation.

How to run Python scripts as a Windows service

Hello everybody! It’s been a while since my last posting, so please have mercy with my rusty writing skills.😊 In this tutorial, I’ll show you how to run Python applications and scripts as a Windows service and the reasons behind it. Let’s dive right into it.

TL;DR

“Why would you even…?”

If those words came into your mind while reading the title, let me stop you right there — you probably don’t really know what a Windows service is. Here’s a quick description.

A Windows service is basically a long-running application in your system. It can be configured to start automatically when you system is booting and also be paused, resumed or started manually.

The most common reason for running a Python script as a service would be to ensure an application runs and starts automatically when you login to your system. For example, you would use this if you have a web application that you want to be running all the time. A Windows service is a great solution for serving Flask or Django applications.

If you want to learn more about Windows services, I’d recommend you to read this.

Setup

Note: It might be ridicuolous to even state this, but Python needs to be pre-installed to run Python scripts.

Download NSSM

You can’t create a Windows service running a Python script by default, but there’s a great tool to solve this problem: NSSM. Simply download the tool and extract it. An installation isn’t required, all you need to do is to add it as a %PATH% environment variable to your system.

Important: Run the following commands as admin!

  1. Create a new service
nssm install “SERVICE_NAME” “PATH_TO_PYTHON.exe” “PATH_TO_SCRIPT.py

If you’re a lazy person like me, searching the Python path would be too much effort. Run this instead:

python -c “exec(\”import sys\nprint(sys.executable)\”)”

The service is installed now and will start automatically every time your computer starts. If you were to open up the Windows Service GUI now, you’d see your service.

2. Add service parameters

The service might already work, but to be safe, log files for the standard output and error output should be configured:

nssm set SERVICE_NAME AppStderr PATH_TO_ERROR_FILE.log
nssm set SERVICE_NAME AppStdout PATH_TO_LOG_FILE.log

Important: The files and paths MUST already exist!

As an alternative, you can open up the GUI with the command

nssm edit SERVICE_NAME

Configure it like shown below.

Edit the files unter the tab “I/O”

3. Start & Test the service

The configuration is complete now. The service can be started now.

nssm start SERVICE_NAME

Note: In case this doesn’t work, try to start it manually in the Windows Services GUI (search for SERVICE_NAME → Start. This should work.)

Some more helpful commands:

nssm stop SERVICE_NAME      //stops the service
nssm restart SERVICE_NAME   //restarts the service
nssm remove SERVICE_NAME    //delete the service

That’s it!🥳 Congratulations, you just learned how to run a Python script in a Windows service using NSSM.

Feel free to share this article if you liked it. Thank you for reading!

Python
Windows Services
Nssm
Python Programming
Flask
Recommended from ReadMedium