# Shutting down Pi from NR

**URL:** <https://discourse.nodered.org/t/shutting-down-pi-from-nr/10616>\
**Category:** General\
**Created:** [28 April 2019 13:15 UTC](https://discourse.nodered.org/t/shutting-down-pi-from-nr/10616 "2019-04-28T13:15:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vazquezjm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vazquezjm/32/7952_2.png) [@vazquezjm](https://discourse.nodered.org/u/vazquezjm)\
**Post date:** [28 April 2019 13:15 UTC](https://discourse.nodered.org/t/shutting-down-pi-from-nr/10616/1 "2019-04-28T13:15:09Z")

</div>

Hi.

I was wondering if there's a way to issue system commands from NR (Raspbian).

Basically, I need to shut it down based on certain condition (reading an MQTT message, GPIO state, etc.)

TIA

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 April 2019 13:18 UTC](https://discourse.nodered.org/t/shutting-down-pi-from-nr/10616/2 "2019-04-28T13:18:52Z")

</div>

Here is an example flow that will give you 5 seconds to confirm the shutdown, You should be able to modify it to your needs.

 ![46%20AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/ece89e26a39880834388af609e6ae21e12bbfc2e.png)

```auto
[{"id":"2c018f5e.11313","type":"tab","label":"shutdown","disabled":false,"info":""},{"id":"2519ee4a.ab1332","type":"exec","z":"2c018f5e.11313","command":"sudo shutdown -h now","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Shutdown the RPi","x":750,"y":380,"wires":[[],[],[]]},{"id":"baec1071.bec978","type":"inject","z":"2c018f5e.11313","name":"Shutdown this Pi","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":220,"wires":[["606c992e.6d6eb8","aa08b69c.ef7b08"]]},{"id":"eb8c8ce9.9e431","type":"inject","z":"2c018f5e.11313","name":"Confirm shutdown","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":380,"wires":[["604071e0.f181f"]]},{"id":"e94145e4.703b58","type":"function","z":"2c018f5e.11313","name":"","func":"if (msg.payload == 1)\n {\n flow.set(\"shutdown\", \"yes\")\n node.status({text:\"Shutdown = yes\"});\n }\nelse if (msg.payload === 0)\n {\n flow.set(\"shutdown\", \"no\")\n node.status({text:\"Shutdown = no\"});\n }","outputs":0,"noerr":0,"x":570,"y":220,"wires":[]},{"id":"606c992e.6d6eb8","type":"trigger","z":"2c018f5e.11313","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":360,"y":220,"wires":[["e94145e4.703b58"]]},{"id":"604071e0.f181f","type":"function","z":"2c018f5e.11313","name":"","func":"var shutdown = flow.get(\"shutdown\") || \"no\";\n\nif ( (msg.payload == 1) && (shutdown == \"yes\") )\n {\n node.status({text:\"Shutting down RPi\"});\n return msg;\n }\nelse\n {\n node.status({text:\"Nothing doing\"});\n return null;\n }","outputs":1,"noerr":0,"x":570,"y":380,"wires":[["f4eead.4206915","2519ee4a.ab1332"]]},{"id":"aa08b69c.ef7b08","type":"function","z":"2c018f5e.11313","name":"Inverter","func":"if (msg.payload == 1)\n{\n msg.payload = 0;\n}\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":300,"wires":[["604071e0.f181f"]]},{"id":"c29f4af1.c246d","type":"comment","z":"2c018f5e.11313","name":"Routine to shutdown this Pi","info":"","x":180,"y":100,"wires":[]},{"id":"deb040ea.60319","type":"comment","z":"2c018f5e.11313","name":"Press the Confirm button within 5 seconds if you do really want to shutdown","info":"","x":330,"y":140,"wires":[]},{"id":"f4eead.4206915","type":"debug","z":"2c018f5e.11313","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":730,"y":300,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![vazquezjm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vazquezjm/32/7952_2.png) [@vazquezjm](https://discourse.nodered.org/u/vazquezjm)\
**Post date:** [28 April 2019 20:17 UTC](https://discourse.nodered.org/t/shutting-down-pi-from-nr/10616/3 "2019-04-28T20:17:06Z")

</div>

Thank you @zenofmud! I wish I had known there was an Exec node 🤭

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [29 April 2019 07:32 UTC](https://discourse.nodered.org/t/shutting-down-pi-from-nr/10616/5 "2019-04-29T07:32:50Z")

</div>

I've been working with @zenofmud on a couple of projects.  
Here's the shutdown routine extended to shutdown a group of RPi(es) at my IoT club.

I'm using the Pi at IP address 192.168.1.142 as the master.  
It can issue a global shutdown to all the Pi(es) or selectively to just one.  
I use these flows at the end of each session of the club so all the Pi(es) are shut down correctly - rather than _some students trying to be helpful by pulling the power leads out !!!!!!_

The flow for the master uses 'node-red-configurable-ping' to ping all the Pi(es) at the club and report the status of each one. Obviously you can remove this part if you're not interested in it.

I've attached two flows: one for the master and one for a slave (e.g. 192.168.1.140).

Hope you find the flows useful.

[global\_shutdown.json](https://discourse.nodered.org/uploads/short-url/iETTyM3XvfwPPBzijQKNm59nfYB.json) (7.3 KB)

[node\_140.json](https://discourse.nodered.org/uploads/short-url/6N1zJir8yIRIAsn7kqRqX8NMpfP.json) (4.3 KB)

---

<div class="post-metadata">

**Author:** ![vazquezjm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vazquezjm/32/7952_2.png) [@vazquezjm](https://discourse.nodered.org/u/vazquezjm)\
**Post date:** [29 April 2019 11:22 UTC](https://discourse.nodered.org/t/shutting-down-pi-from-nr/10616/6 "2019-04-29T11:22:15Z")

</div>

Thank you so much @dynamicdave!

I plan on using this on a custom made "HAT" with an UPS. Based on the state of an IO pin (external PSU will set a pin high). When no electricity, that pin will be low, then it will initialize the shut down sequence.

---

<div class="post-metadata">

**Author:** ![kenkoknz](https://avatars.discourse-cdn.com/v4/letter/k/58956e/32.png) [@kenkoknz](https://discourse.nodered.org/u/kenkoknz)\
**Post date:** [6 May 2019 22:15 UTC](https://discourse.nodered.org/t/shutting-down-pi-from-nr/10616/7 "2019-05-06T22:15:12Z")

</div>

Hi Zen and Dave, I cant seem to import the flows. Copy and paste in import clipboard and import, but bank?

---

<div class="post-metadata">

**Author:** ![kenkoknz](https://avatars.discourse-cdn.com/v4/letter/k/58956e/32.png) [@kenkoknz](https://discourse.nodered.org/u/kenkoknz)\
**Post date:** [6 May 2019 22:34 UTC](https://discourse.nodered.org/t/shutting-down-pi-from-nr/10616/8 "2019-05-06T22:34:59Z")

</div>

Hi all,  
Sorry for that, red hearing, fingers trouble, all good.  
Cheers,
