Running a Python Script as a Windows Service: A Guide

Joseph Thomas
5 min readNov 24, 2021

Getting around Windows permissions issues and user logins to make a python script run anytime your computer is on is tricky business. Here I share an easy step-by-step guide on how to do it.

Johnson Martin

There are many reasons you may want to run a python script 24/7 in the background of a Windows computer. For my work, I design and implement field computer systems in remote locations. Running python scripts in the background of the computer is critical for keeping sensor and environmental data flowing to the cloud without disturbances from researchers who log in and out of the same computer.

Specifically, this guide is meant for Windows systems (as doing this on a linux computer is extremely easy) and is also meant to serve as an alternative to the traditional Windows Task Scheduler (which I’ve found to be wrought with issues of user permissions, log ins, and just plain failing sometimes).

NOTE — This guide assumes the following:
- You already have python properly installed on your computer.
- You are working from an administrator level account on the computer.
- You have a basic working knowledge of python coding and scripting.

1. Generate a Python Virtual Environment

Open command prompt (admin mode)

Create a virtual environment right in the C:\ drive on your computer.

python -m venv C:\virtual_environment_name

Activate the virtual environment.

C:\virtual_environment_name\Scripts\activate

Install the packages required for your script, in the activated virtual environment.

example: pip3 install pandas

2. Download NSSM

NSSM or the ‘Non-Sucking Service Manager’ is a program that was made specifically for tasks like this and is lightweight, free, and very user friendly.

Download NSSM here

Once downloaded, extract the zip file and copy the .exe (from either the win32 or win64 folder depending on your system) into the virtual environment folder.

NSSM does not need to be installed. It will simply run when you ask it to.

3. Place your Python script into the virtual environment folder

Copy and paste your .py script into the virtual environment folder. I usually paste mine into the “Scripts” sub-folder, but anywhere will work fine.

Really Important Note — READ THIS!!

Make sure the python script you select is designed to run 24/7 without crashing your computer, overloading your hard drive, or otherwise messing up your system.
THINK THIS THROUGH BEFORE YOU KEEP MOVING FORWARD.
Also, this process will not run your script more than once, so if you are trying to do a repetitive task like collect sensor data once an hour, build this time delay into your loop. Beware of loops though, as a loop without delays or time based triggers will run constantly and may cause issues with your ram / hard drive / cpu or otherwise wreak havoc on your computer. Know exactly how your script will behave before you install it as a service.

4. Start NSSM

Open command prompt again in admin mode.

Open the NSSM GUI

C:\virtual_environment_name\nssm.exe install

Now you are in the NSSM GUI and can fill out the settings appropriate to your application.

Critical settings for python scripts are:

  • Path: C:\virtual_environment_name\Scripts\python.exe
  • Startup directory: C:\virtual_environment_name\Scripts\
  • Arguments: C:\virtual_environment_name\Scripts\test_script.py
  • Service name: [whatever you want to name it]

Simply adjust the virtual environment folder, python script, and service name.

All other settings can be left at the default values unless you wish to make a change for your specific application. More information about the different options are here.

5. Install the service

Once you’ve adjusted the settings in the GUI to your liking, click install service. This officially installs the python script as a Windows service!

Start the service.

Once installed, start the service using the following command:

C:\virtual_environment_name\nssm.exe start [service name]

Now your service should be running! Check on it by going to the Task Manager or using the following command:

C:\virtual_environment_name\nssm.exe status [service name]

Stop the service (optional)

To stop the service if it is causing problems or if you want to adjust the python script, use the following command:

C:\virtual_environment_name\nssm.exe stop [service name]

Uninstall the service (optional)

To get rid of the service all together use the following command:

C:\virtual_environment_name\nssm.exe remove [service name]

Final Note:

It is best practice to keep all script outputs contained within the virtual environment folder. If you don’t do this (if you try to output something to a specific user’s desktop or documents folder) there is no guarantee that the outputs will actually make it to where they need to go. Keep everything in the virtual environment folder for good measure.

That’s it!

There you have it. A python script installed as a Windows service using NSSM. The script will now run if no user is logged in or if any user is logged in and will start when the PC is turned on (assuming that setting was selected in the ‘Log on’ tab of the GUI).

--

--

Joseph Thomas

Writing about a wide range of topics in my free time.