Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active March 14, 2024 13:29
Show Gist options
  • Star 75 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save magnetikonline/2217fd95cf15a0324696 to your computer and use it in GitHub Desktop.
Save magnetikonline/2217fd95cf15a0324696 to your computer and use it in GitHub Desktop.
NSSM - the Non-Sucking Service Manager cheatsheet.

NSSM cheatsheat

See: NSSM - the Non-Sucking Service Manager

Create a new service

nssm.exe install SERVICE_NAME "C:\path\to\exe\or\bat\file.ext" "argument1 argument2"

List parameters for a service

nssm.exe get SERVICE_NAME *

Set a parameter for a service

nssm.exe set SERVICE_NAME PARAMETER_NAME PARAMETER_VALUE
nssm.exe set SERVICE_NAME Description "My service description."
nssm.exe set SERVICE_NAME Start SERVICE_AUTO_START
nssm.exe set SERVICE_NAME AppExit Default Exit
nssm.exe set SERVICE_NAME AppStdout "C:\log\service-output.log"

Parameter list

AppAffinity
AppDirectory
AppEnvironment
AppEnvironmentExtra
AppExit
Application
AppNoConsole
AppParameters
AppPriority
AppRestartDelay
AppRotateBytes
AppRotateBytesHigh
AppRotateFiles
AppRotateOnline
AppRotateSeconds
AppStderr
AppStderrCreationDisposition
AppStderrFlagsAndAttributes
AppStderrShareMode
AppStdin
AppStdinCreationDisposition
AppStdinFlagsAndAttributes
AppStdinShareMode
AppStdout
AppStdoutCreationDisposition
AppStdoutFlagsAndAttributes
AppStdoutShareMode
AppStopMethodConsole
AppStopMethodSkip
AppStopMethodThreads
AppStopMethodWindow
AppThrottle
DependOnGroup
DependOnService
Description
DisplayName
ImagePath
Name
ObjectName
Start
Type
@mh-cbon
Copy link

mh-cbon commented May 16, 2016

+1 very useful addition to the original doc.

@clembac
Copy link

clembac commented Feb 17, 2017

+1 very useful thks

@savikko
Copy link

savikko commented Jun 5, 2017

One hint also, if you want to set recovery parameters, i think sc -command is quite nice:

sc failure "ServiceName" actions= restart/60000/restart/60000/restart/60000 reset= 86400

This will restart service on first, second and subsequent failures after 1 minute (60000 ms) and will reset fail count after 1 days (86400 seconds).

@girishlc
Copy link

girishlc commented Jun 6, 2017

Hi,

May I know how to switch the directory in NSSM, I need to run my service from particular folder.

After first line,
CD "C:\Program Files\nssm-2.24\win64"

I need to switch to the folder where I can run my service from there, tried with AppDirectory but no luck.. :(

@RamazanBIYIK
Copy link

Folks. I have a .net core console app. when ı use NSSM,a nuget package has been deleting automatically and stayed like that until ı stop service. Do you have any suggestion?

@matthew-walter-works
Copy link

Outstanding, very useful +1

@redtopia
Copy link

redtopia commented Jan 11, 2019

Super helpful! May I suggest adding a section for registry values that includes the cmd for setting them, like: Reg Add Regkey /v RegValue /t RegType /d data - maybe something like:

:: Set number of milliseconds to wait between restarts
Reg Add HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppRestartDelay /v 2000 /t REG_DWORD

@wagostin
Copy link

wagostin commented Aug 7, 2019

I would like to silently install a service for WildFly in a single command and include the user and password to run the service.

This is what I have so far:

nssm install Wildfly15 D:\wildfly-15.0.1.Final\bin\standalone.bat "-b=127.0.0.1 --debug"

This is OK. It opens the dialog box. Can nssm me used silently and can you add logon username and password?

@magnetikonline
Copy link
Author

I'm not sure @wagostin, I guess you're meaning you wish to control the "Run as" user for the service?

You're probably going to want to have a service account already part of your OS, which is then used by nssm as the run as user. I haven't touched nssm in years, so not even sure if that's possible.

@Codearella
Copy link

Also include:
nssm remove SERVICE_NAME

@dybxin
Copy link

dybxin commented May 28, 2021

i have a question, how to start multiple exe files at once? @magnetikonline

before i start exe file in cmd windows one by one, this is very stupid.

Because there is a dependency on the related exe startup, it has to be chain-started. Do you have relevant experience?

Due to the requirement that it cannot be deployed on docker and cannot be started in the cmd window, it must be packaged as a service through nssm。

@magnetikonline
Copy link
Author

@dybxin you'll probably have to make a PowerShell/batch file and execute that? Otherwise treat each exe as an isolated service.

@dybxin
Copy link

dybxin commented May 28, 2021

@dybxin you'll probably have to make a PowerShell/batch file and execute that? Otherwise treat each exe as an isolated service.

likely: the following demo.bat file.

start /d "E:\minio\minio.exe" minio.exe server E:\minio\data

ping /n 10 127.0.0.1>nul

start /d "E:\demo\a.exe" a.exe -format=http

ping /n 10 127.0.0.1>nul

...

or

SETLOCAL
SET PATH="E:\demo\minio"

start /wait minio.exe server E:\demo\minio\data

SETLOCAL
SET PATH="E:\demo\xxx"

start /wait a.exe -format=http

start /wait b.exe -format=http

start /wait c.exe -format=http

@jtcoyle
Copy link

jtcoyle commented Aug 13, 2021

I need to be able to stop and start the service under a standard user account. I have tried to use the runas command (See Below) but I have not had any luck to get it to work.

C:\windows\system32>runas /user:server\support /savecred "c:\kds\nssm stop KDS_MANAGER"
Attempting to start c:\kds\nssm stop KDS_MANAGER as user "server\support" ...

I see a window open and close very fast but the service does not stop

If I use "nssm stop KDS_MANAGER" on the same command prompt the service stops but this only works if the CMD prompt is run as administrator or I need to provide credentials. I am trying to get this to run without having to enter admin credentials.

I have also tried using batch files with no luck.

Any help to solve this would be greatly appreciated.

Regards,

Joe

@moeshin
Copy link

moeshin commented Jan 22, 2022

How to fix program not executing at fast startup (fast boot)?
It's normal to restart windows. But shutdown and then on, the program will not be executed.

@gonbaum
Copy link

gonbaum commented Dec 30, 2022

My node app running through nssm is restarting, again and again, every ~20 secs, but not showing any error. Any idea of which could be the cause? I'm running nssm start <service> through a cmd script in a cmd prompt with administrator rights.

@alexqfredrickson
Copy link

I would like to silently install a service for WildFly in a single command and include the user and password to run the service.

This is what I have so far:

nssm install Wildfly15 D:\wildfly-15.0.1.Final\bin\standalone.bat "-b=127.0.0.1 --debug"

This is OK. It opens the dialog box. Can nssm me used silently and can you add logon username and password?

Not sure, but you can silently remove services. nssm remove <servicename> confirm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment