How to run Python program as a service on Windows

Yash
3 min readFeb 23, 2021

You may have read How to run Python script in background [automatically] on Windows Startup if not, you can check it out now. Now running the program as a system service instead of launching as an application on Startup has some advantages as well as disadvantages. Let’s see everything in detail :

What is a service ?

A service is kind of an application. The difference being that a service runs in the background and does not have a user interface. Services can be started during the system boot, before other programs and even before you log in.

So what good are services for ?

Services can be used for multiple applications like web serving, event logging, file serving, printing or error reporting.

There are obviously more than one methods to run your python program as a service on windows startup automatically, but here, we will be talking about using NSSM or Non-Sucking-Service-Manager.

This article is noob-friendly, written in a way such that even if you just know a hello-world program on python, you’ll be able to understand all of the things covered below.

Step 1. Making an executable file from your .py file.

We will be using Pyinstaller for this.

PyInstaller freezes (packages) Python applications into stand-alone executables, under Windows, GNU/Linux, Mac OS X, FreeBSD, Solaris and AIX.

Basically Pyinstaller reads all the requirements of your program and converts it to a stand-alone application, so that it can run on any system without installing any other dependencies (including python).

  • To install pyinstaller, the below instruction in command prompt :
pip install pyinstaller
  • To make an .exe file, go to your program’s directory and run:
pyinstaller yourprogram.py --onefile

This generates a single-file executable that you can distribute and users do not need to install any particular version of Python or any modules. In fact they do not need to have Python installed at all.

Step 2. Setting up using NSSM

  • Download latest release of NSSM from the official site.
  • Add nssm.exe to your PATH or simply copy the nssm.exe to your program’s directory.
  • Open a command prompt in the same directory and enter. You can enter any name in place of [serviceName]
nssm.exe install [serviceName]

Now NSSM will open up a GUI for you to configure the service. Mandatory to select at least the Path, Startup Directory and Service name. You can check other settings like Actions when the service stops, priority, automatic/manual startup, etc. If you prefer configuring with command line instead of GUI, do check this.

To check if your service has been installed properly, run

nssm.exe start [serviceName]

If this gives a success message, you are all done. You can even check the service running in Task Manager to verify.

Bonus : If you want are planning to make a installer, you can add this to your python file so that your end user does not need to manually perform all the steps. Make sure nssm.exe is in the same program directory.

import os
cp=os.path.abspath(os.getcwd())
os.system("nssm install [serviceNm] '{}/[progName].exe'".format(cp))
os.system("nssm set [serviceNm] AppExit Default Exit")
os.system("nssm set [serviceNm] AppPriority REALTIME_PRIORITY_CLASS")
os.system("nssm start [serviceNm]")

The above code will make a new service named [serviceNm], which will execute [progName].exe with the highest priority upon startup, and stop the service when script is executed once/exits.

The only drawback of this is that you won’t be able to interact with users. If that is a requirement of your program, you should check this article. The program may take some time to start, but you’ll be able to make it interactive.

--

--