Reboot node red under Windows

Do you perhaps have an example of a powershell or a wsh command / script.
I've been googling but can't find it and I'm not that good at scripting.

Never said I was a "fan" either. However, yes, it is working for me.

Yes, exactly right. You should never have to reboot a machine to restart node-red.

No, they really aren't. The best way (most efficient and effective with minimal overheads) is to use a service wrapper. This is exactly the same on Linux/Mac as well.

Then you need to:

  1. track the PID so that you can kill the process
  2. have a way to restart the process

I've listed several ways of doing that. None are especially nice to set up but once done they should all work.

I will see if I can come up with something.

But, as I've said, the "correct" way to do this is to wrap Node-RED into a service. I will see if I can come up with that as well.

Don't hold your breath though, I'm busy with lots of work and family things right now.

I understand. For now, I am working with restarting windows. If there is a cleaner way to only close NR, it would be nice. Thanks to everyone for thinking along and learning moments.

I don't think you've quite thought it through fully.

In settings.js:

    functionGlobalContext: {
        pid: process.pid,
    },

Flow:

[{"id":"abe9f95d.40d2f8","type":"comment","z":"fb0c842.c87fc78","name":"Kill Node-RED","info":"","x":140,"y":1160,"wires":[]},{"id":"70c5a14.d64746","type":"inject","z":"fb0c842.c87fc78","name":"","topic":"","payload":"pid","payloadType":"global","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":1220,"wires":[["93276533.cf5bd8"]]},{"id":"d9a7bcc.f8eb44","type":"debug","z":"fb0c842.c87fc78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":490,"y":1180,"wires":[]},{"id":"93276533.cf5bd8","type":"exec","z":"fb0c842.c87fc78","command":"taskkill /F /PID ","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":300,"y":1220,"wires":[["d9a7bcc.f8eb44"],["f6d9f24.922fa1"],["3c294541.8bb99a"]]},{"id":"f6d9f24.922fa1","type":"debug","z":"fb0c842.c87fc78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":490,"y":1220,"wires":[]},{"id":"3c294541.8bb99a","type":"debug","z":"fb0c842.c87fc78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":490,"y":1260,"wires":[]}]

That lets you kill off the running version of Node-RED.

However, you asked to restart it so this is only the easy bit. To restart you should just create a shortcut to the command that you use to run it in the first place.

Other than the exec command you run, this is no different on other platforms. Indeed, it would be trivial to add a function that formatted the correct command dependent on the OS.

Or, going right back to my original post, you could change the settings entry to actually include the full command rather than just the PID.

As I've previously mentioned, it is the restart that is more complex because once you've killed off the process that started Node-RED.


As it happens, as I was writing that, I had a brainwave. The following flow with the settings.js amendment above will do exactly what you want. You will need to change the "Start Node-RED" exec command as your start will be different to mine. You may also be able to fine-tune the delay.

[{"id":"abe9f95d.40d2f8","type":"comment","z":"fb0c842.c87fc78","name":"Kill Node-RED","info":"","x":140,"y":1160,"wires":[]},{"id":"70c5a14.d64746","type":"inject","z":"fb0c842.c87fc78","name":"","topic":"","payload":"pid","payloadType":"global","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":1220,"wires":[["93276533.cf5bd8","a792c298.b4cd4"]]},{"id":"d9a7bcc.f8eb44","type":"debug","z":"fb0c842.c87fc78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":490,"y":1180,"wires":[]},{"id":"93276533.cf5bd8","type":"exec","z":"fb0c842.c87fc78","command":"taskkill /F /PID ","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":300,"y":1220,"wires":[["d9a7bcc.f8eb44"],["f6d9f24.922fa1"],["3c294541.8bb99a"]]},{"id":"f6d9f24.922fa1","type":"debug","z":"fb0c842.c87fc78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":490,"y":1220,"wires":[]},{"id":"3c294541.8bb99a","type":"debug","z":"fb0c842.c87fc78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":490,"y":1260,"wires":[]},{"id":"a792c298.b4cd4","type":"delay","z":"fb0c842.c87fc78","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":320,"y":1360,"wires":[["4f5de01b.4fb61"]]},{"id":"98924dcb.68d78","type":"debug","z":"fb0c842.c87fc78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":1320,"wires":[]},{"id":"4f5de01b.4fb61","type":"exec","z":"fb0c842.c87fc78","command":"START /B npm start","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Start Node-RED","x":500,"y":1360,"wires":[["98924dcb.68d78"],["f8f6ce5e.791a2"],["aab5c63a.92b158"]]},{"id":"f8f6ce5e.791a2","type":"debug","z":"fb0c842.c87fc78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":1360,"wires":[]},{"id":"aab5c63a.92b158","type":"debug","z":"fb0c842.c87fc78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":1400,"wires":[]}]

Note that one potential issue with this approach is that it will restart Node-RED in the background so you won't easily be able to see any log output unless the command redirects it to a file.

Sorry, that didn't actually work as I should have realised had I had a chance to think it through further.

Blockquote[{"id":"59276a55.08e0a4","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"14a8f8d.6770a07","type":"comment","z":"59276a55.08e0a4","name":"Kill Node-RED","info":"","x":430,"y":240,"wires":},{"id":"16100712.f02989","type":"inject","z":"59276a55.08e0a4","name":"","topic":"","payload":"pid","payloadType":"global","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":440,"wires":[["b09bfa04.9901e8","d8646542.2db2d8"]]},{"id":"83ee4f29.e5a9e","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":260,"wires":},{"id":"ee1662f7.e2a9c","type":"exec","z":"59276a55.08e0a4","command":"taskkill /F /PID ","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":590,"y":300,"wires":[["83ee4f29.e5a9e"],["ee77bd07.30d47"],["5cc8d0b9.f555b"]]},{"id":"ee77bd07.30d47","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":300,"wires":},{"id":"5cc8d0b9.f555b","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":340,"wires":},{"id":"b09bfa04.9901e8","type":"delay","z":"59276a55.08e0a4","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":430,"y":300,"wires":[["ee1662f7.e2a9c"]]},{"id":"da1bcc6c.b705d","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":400,"wires":},{"id":"53c839f7.b3e2f8","type":"exec","z":"59276a55.08e0a4","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Start Node-RED","x":790,"y":440,"wires":[["da1bcc6c.b705d"],["82724e35.66a83"],["dff41b9.7cbdbe8"]]},{"id":"82724e35.66a83","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":440,"wires":},{"id":"dff41b9.7cbdbe8","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":480,"wires":},{"id":"d8646542.2db2d8","type":"function","z":"59276a55.08e0a4","name":"Create start + log","func":"timestamp = new Date().getTime();\nmsg.payload = "START /B node-red -v > nodered.log"+ timestamp + " 2>&1"\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":420,"wires":[["53c839f7.b3e2f8"]]}]

Thanks for the help I have adjusted the flow a little and now it works for me. NR is closed and I start a new session in advance. I send my debug data to a log file with a timestamp. I think this is a nice solution. Perfect, this kind of brain waves occasionally.

I believe it goes wrong with my flow that I have added. I have come across something several times on this forum about adding flows. Will look it up.

I thought it would look like this

Unfortunately, I can't import that flow as it says it is invalid.

Having slept, I realise that you could use the flow to create the start command FIRST before killing off the current flow. Having a delay in the start command would let everything work correctly.

If I get a chance, I'll write a simple node.js command line script that does it all and that you could add to your package.json file so that you could do something like npm run restartNR - which you could do from an exec node of course.

[{"id":"59276a55.08e0a4","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"14a8f8d.6770a07","type":"comment","z":"59276a55.08e0a4","name":"Kill Node-RED","info":"","x":430,"y":240,"wires":},{"id":"16100712.f02989","type":"inject","z":"59276a55.08e0a4","name":"","topic":"","payload":"pid","payloadType":"global","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":440,"wires":[["b09bfa04.9901e8","d8646542.2db2d8"]]},{"id":"83ee4f29.e5a9e","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":260,"wires":},{"id":"ee1662f7.e2a9c","type":"exec","z":"59276a55.08e0a4","command":"taskkill /F /PID ","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":590,"y":300,"wires":[["83ee4f29.e5a9e"],["ee77bd07.30d47"],["5cc8d0b9.f555b"]]},{"id":"ee77bd07.30d47","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":300,"wires":},{"id":"5cc8d0b9.f555b","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":340,"wires":},{"id":"b09bfa04.9901e8","type":"delay","z":"59276a55.08e0a4","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":430,"y":300,"wires":[["ee1662f7.e2a9c"]]},{"id":"da1bcc6c.b705d","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":400,"wires":},{"id":"53c839f7.b3e2f8","type":"exec","z":"59276a55.08e0a4","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Start Node-RED","x":790,"y":440,"wires":[["da1bcc6c.b705d"],["82724e35.66a83"],["dff41b9.7cbdbe8"]]},{"id":"82724e35.66a83","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":440,"wires":},{"id":"dff41b9.7cbdbe8","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":480,"wires":},{"id":"d8646542.2db2d8","type":"function","z":"59276a55.08e0a4","name":"Create start + log","func":"timestamp = new Date().getTime();\nmsg.payload = "START /B node-red -v > nodered.log"+ timestamp + " 2>&1"\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":420,"wires":[["53c839f7.b3e2f8"]]}]

22/5000

Another try.

Please wrap the code in triple back-ticks

[{"id":"59276a55.08e0a4","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"14a8f8d.6770a07","type":"comment","z":"59276a55.08e0a4","name":"Kill Node-RED","info":"","x":430,"y":240,"wires":[]},{"id":"16100712.f02989","type":"inject","z":"59276a55.08e0a4","name":"","topic":"","payload":"pid","payloadType":"global","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":440,"wires":[["b09bfa04.9901e8","d8646542.2db2d8"]]},{"id":"83ee4f29.e5a9e","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":260,"wires":[]},{"id":"ee1662f7.e2a9c","type":"exec","z":"59276a55.08e0a4","command":"taskkill /F /PID ","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":590,"y":300,"wires":[["83ee4f29.e5a9e"],["ee77bd07.30d47"],["5cc8d0b9.f555b"]]},{"id":"ee77bd07.30d47","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":300,"wires":[]},{"id":"5cc8d0b9.f555b","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":340,"wires":[]},{"id":"b09bfa04.9901e8","type":"delay","z":"59276a55.08e0a4","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":430,"y":300,"wires":[["ee1662f7.e2a9c"]]},{"id":"da1bcc6c.b705d","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":400,"wires":[]},{"id":"53c839f7.b3e2f8","type":"exec","z":"59276a55.08e0a4","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Start Node-RED","x":790,"y":440,"wires":[["da1bcc6c.b705d"],["82724e35.66a83"],["dff41b9.7cbdbe8"]]},{"id":"82724e35.66a83","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":440,"wires":[]},{"id":"dff41b9.7cbdbe8","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":480,"wires":[]},{"id":"d8646542.2db2d8","type":"function","z":"59276a55.08e0a4","name":"Create start + log","func":"timestamp = new Date().getTime();\nmsg.payload = \"START /B node-red -v > nodered.log\"+ timestamp + \" 2>&1\"\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":420,"wires":[["53c839f7.b3e2f8"]]}]
[{"id":"14a8f8d.6770a07","type":"comment","z":"59276a55.08e0a4","name":"Kill Node-RED","info":"","x":430,"y":240,"wires":[]},{"id":"16100712.f02989","type":"inject","z":"59276a55.08e0a4","name":"","topic":"","payload":"pid","payloadType":"global","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":440,"wires":[["d8646542.2db2d8","ee1662f7.e2a9c"]]},{"id":"83ee4f29.e5a9e","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":260,"wires":[]},{"id":"ee1662f7.e2a9c","type":"exec","z":"59276a55.08e0a4","command":"taskkill /F /PID ","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":590,"y":300,"wires":[["83ee4f29.e5a9e"],["ee77bd07.30d47"],["5cc8d0b9.f555b"]]},{"id":"ee77bd07.30d47","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":300,"wires":[]},{"id":"5cc8d0b9.f555b","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":340,"wires":[]},{"id":"da1bcc6c.b705d","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":400,"wires":[]},{"id":"53c839f7.b3e2f8","type":"exec","z":"59276a55.08e0a4","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Start Node-RED","x":790,"y":440,"wires":[["da1bcc6c.b705d"],["82724e35.66a83"],["dff41b9.7cbdbe8"]]},{"id":"82724e35.66a83","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":440,"wires":[]},{"id":"dff41b9.7cbdbe8","type":"debug","z":"59276a55.08e0a4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":980,"y":480,"wires":[]},{"id":"d8646542.2db2d8","type":"function","z":"59276a55.08e0a4","name":"Create start + log","func":"timestamp = new Date().getTime();\nmsg.payload = \"START /B node-red -v > nodered.log\"+ timestamp + \" 2>&1\"\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":420,"wires":[["53c839f7.b3e2f8"]]}]

I removed the delay from the flow. Then it works well too.

FYI I have opted for powershell in this instance

Yep.

$proc = Get-CimInstance Win32_Process -Filter "name = 'node.exe' and commandline like '%red.js%'"
Invoke-CimMethod -InputObject $proc -MethodName Terminate

save this as kill_nodered.ps1 then run it with powershell -File kill_nodered.ps1

Thanks for the example I'm going to try.

1 Like

Is there any update on this matter?
This is really a concern for my application. I built an extensive automatic software update for my node-red application. This all works fine, except the last step: restarting node-red.
Node-red (v1.1.2) is running as an nssm service on a windows 10 system.

All the software update part is wrapped in a powershell script. This script is called by the exec node in my flow. To restart node-red at the end of my ps script I tried several things, but none of them worked:
The most simple - 1 line native PS command - is
restart-Service $NRservicename

I also tried by executing the external nssm executable with argument restart $NRservicename.

I tried moving all these commands into a batch file, but all with the same effect.
NR service is stopped but not restarted anymore.
This all seems logic as every process triggered by the exec node is killed immediately on stopping the NR service.
But If I start for instance chrome from the exec node, the application remains active after killing the NR service.
So what makes the difference between a bat file / ps script process that is killed together with NR and chrome is not killed? in other words, how can I find an elegant solution for the very simple task of restarting the NR service from within the flow

UPDATE:
found a reasonable workaround solution:
added a 2nd ps script that is called from the first script and that is not killed by stopping the NR service.
so: the first script that is called by the exec node contains the following lines:

#stop and start NR service
Write-Host "4. restart NR service"
#call external ps script for delayed restarting of NR service
start powershell .\StartNRservice.ps1
Start-Sleep -s 1
Stop-Service $ServiceName 

this second script that is called (StartNRservice.ps1) contains the following code:

Write-Host "(re)starting NR with some delay"
Start-Sleep -s 5
Start-Service $ServiceName

could be refined by replacing the sleep timer by querying the state of the service but this is enough for my application.

If you start NR using the Windows Task Scheduler, you should be able to get it to restart automatically when you shut it down.

Alternatively, use PM2 or nodemon which can be set to auto-restart.