Restart node-RED via dashboard

Just tried running node-red-restart through an exec node, and it works well. It shuts down and restarts gracefully according to NR log. (Raspberry Pi installed via official install script)
Useful to use remotely or to restart NR after updating nodes, without resorting to cmd prompt.

restart

[{"id":"8497fd50.d66e1","type":"exec","z":"4487e413.bb781c","command":"sudo ","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"System Command","x":720,"y":370,"wires":[[],[],[]]},{"id":"c8687228.7cd88","type":"inject","z":"4487e413.bb781c","name":"Restart node-RED","topic":"","payload":"node-red-restart","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":480,"y":370,"wires":[["8497fd50.d66e1"]]}]
11 Likes

Does this work if running NR in container?
I tried and getting error: 127

Kanke,

This post was over a year old so the change of a reply is small.
However, I had a similar requirement and got it to work for my Debian based box. (Not a Pi)
This might work for you, but I am not sure what OS you are running.
Try this:

[{"id":"a7eb189d.f5e538","type":"comment","z":"cd4d0228.faf14","name":"Warm reboot","info":"","x":410,"y":310,"wires":[]},{"id":"a06415d6.7ed3c8","type":"function","z":"cd4d0228.faf14","name":"Request Token","func":"msg.payload = {\n    \"client_id\": \"node-red-editor\",\n    \"grant_type\": \"password\",\n    \"scope\": \"*\",\n    \"username\": \"engineer\",\n    \"password\": \"Pa$$w0rd\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":360,"wires":[["47aa4d07.5bae94"]]},{"id":"649f96c6.bc9128","type":"inject","z":"cd4d0228.faf14","name":"Manual reload","repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":210,"y":360,"wires":[["a06415d6.7ed3c8"]]},{"id":"47aa4d07.5bae94","type":"http request","z":"cd4d0228.faf14","name":"Token","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://localhost:1880/auth/token","tls":"","persist":false,"proxy":"","authType":"","x":570,"y":360,"wires":[["d69beb8b.7008a8"]],"info":"Note:\nEnsure the path is correct\nif httpAdminRoot: '/admin' is activated then \nthis needs to be added to\nhttp://localhost:1880/"},{"id":"d69beb8b.7008a8","type":"function","z":"cd4d0228.faf14","name":"Confirm token","func":"// get the status of the request\nvar status = msg.statusCode;\n\nvar token = '';\nmsg.headers ={};\n\n//let node = feedback;\n\nswitch(status){\n    case 200:\n        node.log(\"Secure restart\");\n        token = JSON.parse(msg.payload);\n        token = 'Bearer '+token.access_token;\n        msg.headers = {\n            \"Authorization\": token,\n            \"Node-RED-Deployment-Type\":\"reload\"\n        }\n//msg.payload =\"\";\n        break;\n    case 204:\n        node.log(\"Secure without restart\");\n        break;\n    case 400:\n        node.warn(\"Bad request\");\n        break;\n    case 401:\n        node.warn(\"Not authorized\");\n        break;\n    case 403:\n        node.warn(\"Forbidden\");\n        break;\n    case 404:\n        node.log(\"Unsecure restart\");\n        msg.headers = {\n            \"Node-RED-Deployment-Type\":\"reload\"\n        }\n        break;\n    case 409:\n        node.warn(\"Version mismatch\");\n        break;\n    case 500:\n        node.error(\"Server Error\");\n        break;\n    default:\n        node.warn(\"Unknown Error\");\n       break;\n}\n\nmsg.payload = \"\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":720,"y":360,"wires":[["ccd455c1.f34318"]],"info":"Restart of Node-Red flows.\nWill check if the action needs security or not.\nNote: if the first 5 attemps return a statuscode 403 'forbidden'\nthen the server will break and only way to recover is then to\nrestart the service"},{"id":"ccd455c1.f34318","type":"http request","z":"cd4d0228.faf14","name":"Restart","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://localhost:1880/flows","tls":"","persist":false,"proxy":"","authType":"","x":880,"y":360,"wires":[[]],"info":"Note:\nEnsure the path is correct\nif httpAdminRoot: '/admin' is activated then \nthis needs to be added to\nhttp://localhost:1880/"}]

Ensure in the 'reload flows' you have the right credentials. In my example it is 'engineer' and 'Pa$$w0rd' . Change it to your credentials.

can any of these two be done by native url

1 restart flows ... eg http://my-node-red:1880/restart-flows ?

2 restart node red ... eg http://my-node-red:1880/restart-node-red ?

I don't see why not, just create a HTTP endpoint http://my-node-red:1880/restart-flows and feed the output from the http in node into the exec node, which will then run the command to restart node-RED.
You of course need to be mindful that it could pose a security problem if the endpoint is a public url, which anyone could use to restart your server.

This thread is about restarting node-RED from the dashboard, and over a year old, but if you have any further questions please start a new thread.