Creating a Windows service using Python, PyInstaller and NSSM



Download "NSSM" from here:
https://nssm.cc/release/nssm-2.24.zip

Unzip the “nssm” folder in the directory:
C:\Users\ashish\Desktop\Workspace\Jupyter\(Installations)

Add the following path to the “PATH” variable:
C:\Users\ashish\Desktop\Workspace\Jupyter\(Installations)\nssm-2.24\win64

To create a service:
C:\windows\system32>nssm install Del_temp_files
Service "Del_temp_files" installed successfully!

Python scripts do get installed but they do not run:
C:\windows\system32>nssm install Py_log_time
Service "Py_log_time" installed successfully!

import time
while True:
    with open('C:/Users/ashish/Desktop/Log.log', 'a') as f:
        time.sleep(3)
        f.write(str(time.time())  + "\n")

Even if you try to launch it using a BAT file:

Contents of “Script.bat”:
python C:\Users\ashish\Desktop\Script.py

To create a Python based service using NSSM (Note: the Python program above did not work with this method):

URL: https://stackoverflow.com/questions/32404/how-do-you-run-a-python-script-as-a-service-in-windows

The simplest way is to use the: NSSM - the Non-Sucking Service Manager:

1: Make download on https://nssm.cc/download

2: Install the python program as a service: Win prompt as admin.
C:\>nssm install My_Win_Service

3: On NSSM´s console:
Path: C:\Users\ashish\AppData\Local\Continuum\anaconda3\python.exe
Startup directory: C:\Users\ashish\AppData\Local\Continuum\anaconda3
Arguments: C:\Users\ashish\Desktop\Script.py

You can find out the value for “Path” using command “where python” in the Windows CMD.

4: Check the created services on “services.msc”

To remove a service:
C:\windows\system32>nssm remove Del_temp_files
Service "Del_temp_files" removed successfully!

The above command disables the service but does not delete it.
To delete a service that has not been automatically removed by a software uninstall you need to edit the registry:

1.       Start the registry editor (regedit.exe)
2.       Move to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services key
3.       Select the key of the service you want to delete.
4.       From the Edit menu select "Delete"
5.       You will be prompted "Are you sure you want to delete this Key" click ‘Yes’
6.       Exit the registry editor.

Another attempt to create a Windows service using ‘pyinstaller’:

If you are using conda (and not ‘pip’) then you will have to use the following commands instead:

conda install -c conda-forge pyinstaller
conda install -c anaconda pywin32

To create an “exe” for your Python program:


(base) C:\Users\ashish\Desktop>pyinstaller --onefile Script.py
39041 INFO: Appending archive to EXE C:\Users\ashish\Desktop\dist\Script.exe
39057 INFO: Building EXE from EXE-00.toc completed successfully.

Now, create a Windows service as follows:

1: Install the python program as a service: Win prompt as admin.
C:\>nssm install My_Win_Service_2

2: On NSSM´s console:
Path: C:\Users\ashish\Desktop\dist\Script.exe


No comments:

Post a Comment