I created Window Service using Non-Sucking Service Manager (NSSM) for python script. If I have to make some changes in python script .Do I only need to replace updated python script at the specified path from where NSSM picks up or do I need to update service as well ? If Service needs to be updated How I can do that ?
Asked
Active
Viewed 631 times
1 Answers
0
I delete and recreate the service (as localadmin):
sc.exe stop <servicename>
sc.exe delete <servicename>
You can also use nssm.exe:
nssm.exe stop <servicename>
nssm.exe remove <servicename>
Enclosed is a powershell script I use to set my python scripts as services, you have to run it as localadmin:
<#
.SYNOPSIS
Installing a python script as a service under windows
.DESCRIPTION
install a recent python https://www.python.org/ftp/python/3.10.1/python-3.10.1-amd64.exe
add python to system path
add a python venv to use (python.exe -m venv <directory to install vens on aka c:\\python\\venvs>)
set $appexe (must have full path to the venv version), $name and $port
You also need nssm.exe in the current directory see https://nssm.cc/download and https://nssm.cc/usage
#>
$pythonexe = "c:\\dist\\venvs\\gitlab-mobj-board\\Scripts\\python"
$appexe = (Get-Command .\main.py).Source
$name = "GitlabBoard"
$pwd = (pwd).Path
$port = 8000
$ip = '0.0.0.0'
write-host "pythonexe:$pythonexe"
write-host "appexe:$appexe"
write-host "name:$name"
write-host "ip:$ip - port:$port"
write-host "pwd:$pwd"
$nssmexe = (Get-Command .\nssm.exe).Path
$command = '"$nssmexe" install "$name" "$pythonexe" "$appexe" --ip=$ip --port=$port'
iex "& $command"
$command = '"$nssmexe" set "$name" AppDirectory "$pwd"'
iex "& $command"
$command = '"$nssmexe" set "$name" Start SERVICE_AUTO_START'
iex "& $command"
write-host "#start service: sc.exe start $name or Start-Service $name"
write-host "#stop service: sc.exe stop $name or Stop-Service $name"
write-host "#delete service: sc.exe delete $name"
Below is output running the script:
.\create-service.ps1
pythonexe:c:\\dist\\venvs\\gitlab-mobj-board\\Scripts\\python
appexe:C:\dist\work\gitlab-mobj-board\main.py
name:GitlabBoard
ip:0.0.0.0 - port:8000
pwd:C:\dist\work\gitlab-mobj-board
Service "GitlabBoard" installed successfully!
Set parameter "AppDirectory" for service "GitlabBoard".
Set parameter "Start" for service "GitlabBoard".
#start service: sc.exe start GitlabBoard or Start-Service GitlabBoard
#stop service: sc.exe stop GitlabBoard or Stop-Service GitlabBoard
#delete service: sc.exe delete GitlabBoard
.\nssm.exe dump gitlabboard
C:\dist\work\gitlab-mobj-board\nssm.exe install GitlabBoard c:\\dist\\venvs\\gitlab-mobj-board\\Scripts\\python
C:\dist\work\gitlab-mobj-board\nssm.exe set GitlabBoard AppParameters "C:\dist\work\gitlab-mobj-board\main.py --ip=0.0.0.0 --port=8000"
C:\dist\work\gitlab-mobj-board\nssm.exe set GitlabBoard AppDirectory C:\dist\work\gitlab-mobj-board
C:\dist\work\gitlab-mobj-board\nssm.exe set GitlabBoard AppExit Default Restart
C:\dist\work\gitlab-mobj-board\nssm.exe set GitlabBoard DisplayName GitlabBoard
C:\dist\work\gitlab-mobj-board\nssm.exe set GitlabBoard ObjectName LocalSystem
C:\dist\work\gitlab-mobj-board\nssm.exe set GitlabBoard Start SERVICE_AUTO_START
C:\dist\work\gitlab-mobj-board\nssm.exe set GitlabBoard Type SERVICE_WIN32_OWN_PROCESS
sc.exe query gitlabboard
SERVICE_NAME: gitlabboard
TYPE : 10 WIN32_OWN_PROCESS
STATE : 1 STOPPED
WIN32_EXIT_CODE : 1077 (0x435)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
sc.exe start gitlabboard
SERVICE_NAME: gitlabboard
TYPE : 10 WIN32_OWN_PROCESS
STATE : 2 START_PENDING
(NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x7d0
PID : 21304
FLAGS
I can then go to http://localhost:8000 and the board is there and it autostarts :-)

MortenB
- 2,749
- 1
- 31
- 35