Skip to content

Instantly share code, notes, and snippets.

@dceejay

dceejay/nssm.md

Last active Apr 14, 2021
Embed
What would you like to do?
Running Node-RED as a service on Windows using NSSM

Running Node-RED as a service on Windows using NSSM

These instructions assume you installed Node-RED from an administrator enabled command prompt using the npm install -g node-red command to do so.

As an administrator, install nssm from https://nssm.cc/download

And then from the directory where you installed nssm, run the commands

mkdir c:\temp
nssm install Node-RED "c:\Users\\"%USERNAME%"\AppData\Roaming\npm\node-red.cmd"
nssm set Node-RED AppDirectory "c:\Users\\"%USERNAME%"\.node-red"
nssm set Node-RED AppParameters "-u c:\Users\\"%USERNAME%"\.node-red > c:\temp\node-red.log"
nssm set Node-RED Description "A wiring tool for the Internet of Things"

You can edit the service using the GUI to the configuration, add extra parameters, etc.

nssm edit Node-RED

The log will be written to c:\temp\node-red.log (see line 3)

For further options read the nssm usage page.

@natcl

This comment has been minimized.

Copy link

@natcl natcl commented Jan 21, 2016

For anybody having troubles with this, make sure you create the C:\temp directory before running the service.

@bloigge

This comment has been minimized.

Copy link

@bloigge bloigge commented Apr 13, 2016

Another way for autostarting NodeRed in PM2 as a Serivce in Windows:

Everything in command-line as Admin:

Install node-red

npm install -g node-red

Install pm2

npm install pm2 -g

Install and config pm2-windows-startup package :

npm install pm2-windows-startup -g
pm2-startup install
pm2 save

Locate the Node-Red red.js File and add it to pm2:

pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js --save

@alex3305

This comment has been minimized.

Copy link

@alex3305 alex3305 commented Dec 3, 2016

Note: Running this from a Powershell prompt will not work. Escaping of quotes will fail and the commands will all get borked.

@qlartpg

This comment has been minimized.

Copy link

@qlartpg qlartpg commented Aug 7, 2017

This will not work if the directory or username have a space in it. It will run, but if you look at the log, it creates new folder after where the space will be. Anyone have solution? Thanks.

Update:
Don't run this:
nssm set Node-RED AppParameters "-u c:\Users\"%USERNAME%".node-red > c:\temp\node-red.log"

run: nssm edit Node-Red
and make sure the parameters is blank and and it worked for me.

@autumnharmony

This comment has been minimized.

Copy link

@autumnharmony autumnharmony commented Aug 22, 2017

Hi, i'm trying to lock windows workstation by executing "rundll32.exe user32.dll,LockWorkStation". I'm using exec node.
In case of nodered is running as service it's failing.
Could someone tell me why ?
https://msdn.microsoft.com/en-us/library/windows/desktop/aa376875(v=vs.85).aspx

The LockWorkStation function is callable only by processes running on the interactive desktop. In addition, the user must be logged on, and the workstation cannot already be locked.

Could someone tell me about alternative ways ?

@KL-AU-S

This comment has been minimized.

Copy link

@KL-AU-S KL-AU-S commented Feb 15, 2019

Hi,
i got it running with nssm and the redirection of the output. I used the following Node-Red AppParameters:
-u c:\Users"%USERNAME%".node-red 2> C:\temp\node-red.log 1>&2
In this case at first stderr will be redirected after that stdout will be redirected to the same file as stderr.

see also: https://support.microsoft.com/de-de/help/110930/redirecting-error-messages-from-command-prompt-stderr-stdout

@wimmatthijs

This comment has been minimized.

Copy link

@wimmatthijs wimmatthijs commented Jun 13, 2019

Another way for autostarting NodeRed in PM2 as a Serivce in Windows:

Everything in command-line as Admin:

Install node-red

npm install -g node-red

Install pm2

npm install pm2 -g

Install and config pm2-windows-startup package :

npm install pm2-windows-startup -g
pm2-startup install
pm2 save

Locate the Node-Red red.js File and add it to pm2:

pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js --save

thanks, works well

@Aklex1

This comment has been minimized.

Copy link

@Aklex1 Aklex1 commented Mar 11, 2020

pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js -save

@aniston

This comment has been minimized.

Copy link

@aniston aniston commented Oct 28, 2020

pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js --save

on W10 that won't work unless preceded by -- hence that becomes

pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js -- --save

My flavor to running node-red as a service was the combination of pm2 and pm2-windows-service

  • pm2-windows-service actually handles only the pm2 as a visible windows service.
  • after that pm2 kicks in and handles it's own configuration.

Sorry to have jumped in on an thread that has to do with the NSSM manager !

@amilanov75

This comment has been minimized.

Copy link

@amilanov75 amilanov75 commented Nov 19, 2020

Hey, I setup the PM2 service described above by @bloggie. Thank you BTW for your solution!

When I was running from the command prompt and I went to safely shut down ie.. ctrl+C, sometimes it would take a good few seconds before it shut down or I'd even have to hit ctrl+c a couple of times before it shut down.

When I stop the service using:
cmd /k pm2 stop C:\Users\ubiquitous\AppData\Roaming\npm\node_modules\node-red\red.js pause
It stops the service almost instantly every single time.
I'm just wondering if the way I am stopping the service is the right way for it to be "safe"

@ELGUEROUEL

This comment has been minimized.

Copy link

@ELGUEROUEL ELGUEROUEL commented Mar 24, 2021

thanks it is working for me
As an administrator, install nssm from https://nssm.cc/download

And then from the directory where you installed nssm, run the commands

mkdir c:\temp
nssm install Node-RED "c:\Users\"%USERNAME%"\AppData\Roaming\npm\node-red.cmd"
nssm set Node-RED AppDirectory "c:\Users\"%USERNAME%".node-red"
nssm set Node-RED AppParameters "-u c:\Users\"%USERNAME%".node-red > c:\temp\node-red.log"
nssm set Node-RED Description "A wiring tool for the Internet of Things"

@Cappe73

This comment has been minimized.

Copy link

@Cappe73 Cappe73 commented Apr 14, 2021

Another way for autostarting NodeRed in PM2 as a Serivce in Windows:

Everything in command-line as Admin:

Install node-red

npm install -g node-red

Install pm2

npm install pm2 -g

Install and config pm2-windows-startup package :

npm install pm2-windows-startup -g
pm2-startup install
pm2 save

Locate the Node-Red red.js File and add it to pm2:

pm2 start C:\Users\<USER-NAME>\AppData\Roaming\npm\node_modules\node-red\red.js --save

thank you so much

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