# Notification when NR is stopped

**URL:** https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742
**Category:** General
**Created:** [21 September 2019 05:17 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742 "2019-09-21T05:17:20Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [21 September 2019 05:17 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/1 "2019-09-21T05:17:20Z")

</div>

I would like to "do some stuff" in my flow when NR is about to be stopped. Is there any event or so that I could use in a function node to get notified for this purpose. I saw in the api @node-red/runtime there is the function stop() but how can I catch the returned message in a function node? Are there any possibilities to subscribe to some callback or event in a function node that NR "is about to be stopped"?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [21 September 2019 05:50 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/2 "2019-09-21T05:50:14Z")

</div>

If the runtime is stopped, it could also be a non-graceful exit.  
Would an external watchdog that watches the node-red process be more helpful to perform some actions ? Or are the actions node-red related ?

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [21 September 2019 06:04 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/3 "2019-09-21T06:04:54Z")

</div>

Good Morning,

My idea is that this is for a planned shutdown. There are maybe certain things that I want the flow to do at the end so to say, so a kind of pre-warning, now shutdown "will happen shortly". I also would like to inform external (Python, what ever) processes or other instances of NR that the "main" NR is shutting down

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [21 September 2019 10:10 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/4 "2019-09-21T10:10:19Z")

</div>

I think the way to do this is to get Node-RED to initiate the shutdown itself by doing exec call to the OS

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [21 September 2019 12:11 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/5 "2019-09-21T12:11:26Z")

</div>

I think I would do this the other way around.

Create a "graceful shutdown" flow which sends an MQTT msg and then waits for a period of time before shutting down Node-RED (or the device depending on what you are trying to achieve).

Then other flows can subscribe to that topic and can take action knowing the time period they have to work with.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [21 September 2019 14:32 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/6 "2019-09-21T14:32:36Z")

</div>

Yes, this I have thought about as well and it is pretty straight forward, even make it react to incoming MQTT msg's like "shutdown now" or whatever

But it does not cover those cases where you stop NR via the node-red-stop script or via systemd. In addition it will not cover situations where NR is restarting by itself or by systemd, at least I have seen this happen from time to time (why this happens is for me still unknown). So I think NR itself should have such a message that is activated like "shutdown pending"...

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 September 2019 14:45 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/7 "2019-09-21T14:45:33Z")

</div>

If it is apparently restarting itself then there is a strong possibility that it has crashed and systemd has restarted it. In which case your suggestion would not help.  
Looking in syslog should enable you to see whether this is the case, I think.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [21 September 2019 14:46 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/8 "2019-09-21T14:46:28Z")

</div>

> [@krambriw](#):
>
> So I think NR itself should have such a message that is activated like "shutdown pending"...

But once you've got to that stage, shutdown would be imminent, and you wouldn't have enough time to do anything further anyway.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [21 September 2019 14:48 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/9 "2019-09-21T14:48:01Z")

</div>

> [@Colin](#):
>
> In which case your suggestion would not help

Correct, yes

> [@Paul-Reed](#):
>
> But once you've got to that stage, shutdown would be imminent

Well, I was thinking NR would offer a slight delay between the msg and the actual shutdown

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 September 2019 14:50 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/10 "2019-09-21T14:50:29Z")

</div>

> [@krambriw](#):
>
> But it does not cover those cases where you stop NR via the node-red-stop script or via systemd

You could modify the systemd script so that when shutting down, rather than killing node red it runs a script that sends the shutdown message via mqtt. With a timeout so that if that doesn't work then eventually it kills it.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [21 September 2019 14:54 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/11 "2019-09-21T14:54:44Z")

</div>

> [@krambriw](#):
>
> I was thinking NR would offer a slight delay

There would be a slight delay, but once it starts the shutdown process is it likely to start a new process running, like doing the functions which you want it to do.

I can only see this working if the shutdown is initiated by a flow, starting with an inject node or dashboard button, and ending with an exec node calling the shutdown.  
Between the two, you could have any number of different functions, like broadcasting a MQTT message, displaying a pop-up, adding a delay, etc.

---

<div class="post-metadata">

### Author: ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)
#### Post date: [21 September 2019 14:56 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/12 "2019-09-21T14:56:15Z")

</div>

OK, I guess, I will do some experiments  
Thank you all for ideas and suggestions!

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [21 September 2019 20:26 UTC](https://discourse.nodered.org/t/notification-when-nr-is-stopped/15742/13 "2019-09-21T20:26:12Z")

</div>

> [@krambriw](#):
>
> But it does not cover those cases where you stop NR via the node-red-stop script or via systemd. In addition it will not cover situations where NR is restarting by itself or by systemd, at least I have seen this happen from time to time (why this happens is for me still unknown). So I think NR itself should have such a message that is activated like "shutdown pending"...

As others have intimated, you need an external monitor for this, Node-RED itself will not be in a position to know what is going on.

One quick way to help create a monitor is to use MQTT with a heartbeat from Node-RED and an LWT. That way, other apps can know whether Node-RED is working or not and take other actions. Though this doesn't help you to do some quick tidying inside Node-RED but I don't think that anything will help you do that if Node-RED is failing for some reason.

With an external monitor, perhaps you could save some flags to a file that your Node-RED flow would read on startup. Then you could possibly do some tidying on restart instead?

It is really hard to give a definitive answer here as we don't really know exactly what you need to clean up.
