Reboot node red under Windows

How can I reboot node-red under windows with a button on my dashboard. I suspect with the exec node but I cannot find the correct command.

Have you googled it?
https://www.google.com/search?q=reboot+windows+from+cmd&oq=reboot+windows+from+cmd&aqs=chrome..69i57j0l5.12421j0j9&sourceid=chrome&ie=UTF-8
EDIT - just re- read your question!
Please ignore!!!

I tried but maybe even googling is not my strongest side. I'll take a look at what you posted

How did you install node-RED, and what are you using to start it after a reboot?
ie - https://nodered.org/docs/faq/starting-node-red-on-boot
(possibly PM2 or NSSM?)

I installed it with npm. I normally start with C: \ Users \ name \ AppData \ Roaming \ npm \ node-red.cmd under the scheduler. I actually wanted a button so that you can restart the node-red server from the screen if there are problems.
I only wanted to reset the node-red server in the first instance.

@TotallyInformation - From previous forum posts, I seem to think you may have some knowledge about this which may help Eric??

Restart of windows worked. I made an exec with command shutdown / r / t 0. This was indeed a bit too much shutdown. After a few minutes I was able to continue. Is a nice feature to close the system from the web page. But not to restart my node-red.

What I do when running manually or under a schedule job is to write the PID to a file on startup. You can do that in settings.js. Then I can use the PID file to kill the process. You could even write out the cmd file with the PID included so that you had a single file that you could execute to kill the process.

Better still however is to run Node-RED as a service. There is a tool that will turn any executable into a service, I forget the name I'm afraid. Then you can create shortcuts to start/stop the service as you want.

As I use Windows as my dev system I don't always leave Node-RED running and I tend to run it from within VScode. However, I also have a cmd shortcut that starts it and leaves the cmd window minimised. So that is easy to open up and ctrl-c to kill it.

So there are a few ways, I use several of them depending on what I am doing.

1 Like

I tried to read my PID, but I cannot get it working.
Is there a possibility to execute the restart flows function under the deploy button of noder-red as a command. This is what I do now if I want to restart my flows. But the user of the web server should not do this through the development environment.

The problem you have is that to restart a process, it has to have a wrapper. On Linux, you typically use systemd. On Windows, you typically have a service. So if you set NR up as a service, you will be able to restart it from a flow - I do that on my Pi's with systemd.

I don't think you can do this manually. If you don't want to set up NR as a service, you will need to use something like PM2 which lets you send commands to restart and can also be called from a flow.

Here is my current code from the start of my settings.js file that writes a PID file.

var fs   = require('fs')

/** Save a PID file so that Node-RED can be easily restarted even when run manually */
const pid = process.pid
console.info('PID: ', pid)
fs.readdir('.', (err, files)=>{
    if (err) throw err

    for (var i = 0, len = files.length; i < len; i++) {
        var match = files[i].match(/.*\.pid/)
        if(match !== null) fs.unlink(match[0], (err) => {
            if (err) throw err
        })
    }

    fs.writeFile(`${pid}.pid`, pid, (err) => {
        if (err) throw err
    })
})

This section of the docs shows you how to run NR from the task scheduler. Combined with the PID file to allow you to kill the process and the setting in the scheduler that restarts on failure, you should be able to restart NR.

https://nodered.org/docs/getting-started/windows#run-node-red-on-startup

Dave documented how to run NR as a Windows service back in 2016:

node-windows lets you roll your own Windows services from node.js apps. This article shows you how to use that with Node-RED specifically.

This StackOverflow question from 2012 gives some other alternatives for running node.js apps as Windows services.

OK thanks. I will set up NR as a service PM2 or NSSm. Are there major differences? I see that they are both used.

pm2 gif this error

H:>pm2 start node-red
internal/modules/cjs/loader.js:638
throw err;
^

Error: Cannot find module 'v8-compile-cache'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object. (C:\Users\emeijer\AppData\Roaming\npm\node_modules\pm2\bin\pm2:7:3)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)

We went this road and although it worked it was a no-go. Windows services are really bad at interacting with the windows desktop environment, even if the flag is checked for it and it runs under an account which profile has been initiated manually (by logging in). Because of the necessity for node-red to automate some softwares around, we chose to start it as a task in the task scheduler.

To restart it, many ways, from a WSH script running WMI command, or some powershell equivalent,...

PM2 has quite an overhead. Running direct as a service is more efficient in that sense. On the other hand, you get some additional features in PM2.

Sorry, not sure what is causing that, you may need to raise an issue against it. Last time I was using it, it worked OK on Windows 10.

Services run under the SYSTEM account. As Node-RED is a server, it runs fine. However, you do need to understand the Windows security model in order to be able to access some resources and the default Node-RED installation will likely need some file/folder security tweaks. Not sure if NSSm or similar lets you set the owning user away from SYSTEM to a real user I'm afraid.

Certainly it depends on what you are doing with it. Many use cases would be fine.

As mentioned, you can do this if you track the PID but you still need an external mechanism to enable it to restart. Also as mentioned, the task scheduler should be able to do this for you, you just need to set the job up correctly.

The downside of this approach is that NR will be difficult to stop if you don't always want it running. Whereas that is easy when using a service.

If you want something in between then PM2 or similar would be my recommendation, just be aware of the overheads of PM2 itself, especially if running on a resource constrained device.

I quite often use nodemon rather than PM2 when developing nodes. It is much simpler and more lightweight but doesn't have all of the features. It can auto-restart on file changes and be manually restarted from the console by typing rs but that's about all.

nodemon works.
Can I send the rs command with an exec node. I have tested different versions, but I still have no results. If I type it in the command line the service is restarted.

I don't believe so, no. I think only PM2 lets you control it externally though I might be wrong, I've not checked the docs.

Okay, then I try to get PM2 working and otherwise I will reboot with shutdown / r / t 0. This should only happen in exceptional cases that something has crashed.
Thank you for the support.

i'm not a fan of pm2 under windows... is it really working ?

you dont need to reboot the machine lol just run an exec node that calls pm2 restart node-red

In the end I would definintely prefer either a powershell or a wsh command/script , instead of relying on a third party product (like pm2). The OP was about restarting node-red on windows , not running node-red as a service on windows. These are two different questions.