# How to stop NodeRED from inside a flow?

**URL:** <https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693>\
**Category:** General\
**Created:** [16 November 2022 09:18 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693 "2022-11-16T09:18:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![wolfred](https://avatars.discourse-cdn.com/v4/letter/w/838e76/32.png) [@wolfred](https://discourse.nodered.org/u/wolfred)\
**Post date:** [16 November 2022 09:18 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/1 "2022-11-16T09:18:51Z")

</div>

I need to run NodeRED in batch mode (headless) in a Docker container. Works perfect but I cannot terminate NodeRED anymore.

NodeRED ist started (as root) with

```
/usr/local/bin/node $NODE_OPTIONS node_modules/node-red/red.js --userDir /data $FLOWS &

```

I tried to kill NodeRED with the exec-Node and `SIGTERM` / `SIGKILL`. But this does not work.

How can the entire NodeRED process be stopped from a flow?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 November 2022 09:23 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/2 "2022-11-16T09:23:02Z")

</div>

I don't know anything about `docker` - heard of it, but that's about it.

Sorry, but why do you need to STOP node-red?

But if you want to stop `Node-Red`, why not issue the command:

`node-red-stop`?  
With suitable path in front of it.

---

<div class="post-metadata">

**Author:** ![wolfred](https://avatars.discourse-cdn.com/v4/letter/w/838e76/32.png) [@wolfred](https://discourse.nodered.org/u/wolfred)\
**Post date:** [16 November 2022 09:24 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/3 "2022-11-16T09:24:35Z")

</div>

I have a flow running and at the end of this flow NodeRED has to terminate by itself. There should not be any further command as `node-red-stop` to be performed from outside NodeRED

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 November 2022 09:25 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/4 "2022-11-16T09:25:32Z")

</div>

`exec` node at the end of things happening in the flow.

That enters the command and _kills_ (more like stops) Node-red.

---

<div class="post-metadata">

**Author:** ![wolfred](https://avatars.discourse-cdn.com/v4/letter/w/838e76/32.png) [@wolfred](https://discourse.nodered.org/u/wolfred)\
**Post date:** [16 November 2022 09:32 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/5 "2022-11-16T09:32:51Z")

</div>

No, the exec node does not work. The parameters for the exec node are:

```
msg.kill="SIGKILL";
msg.pid=global.get("PID"); 

```

The global PID is set to the process ID of NodeRED at the beginning of the flow.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 November 2022 09:36 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/6 "2022-11-16T09:36:29Z")

</div>

Well, it maybe doesn't work because it needs the `sudo` password.

I'm not an expert, but there is a way to get around that.  
Then you can simply enter that command and it should work.

But I'm not an expert.

In the `exec` node you need to put this:  
`echo myPassword | sudo -S node-red-stop`

Where `myPassword` is your actual password.

It works.

Just tested it.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 November 2022 09:49 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/7 "2022-11-16T09:49:22Z")

</div>

The exec node cannot be used to kill processes it didn't originally spawn itself.

One approach will be to write a script that does the process kill, and then exec that script from Node-RED.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 November 2022 09:51 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/8 "2022-11-16T09:51:15Z")

</div>

Just to check though: Could you confirm that the way I said would work?  
Sorry again just wanting to make sure what I think is correct.

Of course it would be a lot easier if the `node-red-stop` command was _attributed_ to not need the password.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 November 2022 09:52 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/9 "2022-11-16T09:52:48Z")

</div>

> [@Trying\_to\_learn](#):
>
> Sorry again just wanting to make sure what I think is correct.

In this instance, no it would not work because they are running node-red directly, rather than running it as a service (which is what `node-red-start`/`node-red-stop` control).

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 November 2022 09:55 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/10 "2022-11-16T09:55:22Z")

</div>

Thanks.

Just mentioning then:

AFAIK, my system uses `systemd` and node-red auto-starts.

I got an `exec` node and put that _trick_ in with my real password.

Connected an `inject` node to the `exec` node, and pressed it.  
Node-red stopped.  
Well, I got a message at the top of my screen: `Lost connection to server`.  
So I guess it stopped.

```auto
[{"id":"260ff9050f0901f0","type":"exec","z":"0918ee609bf69fc7","command":"echo \"my real password\" | sudo -S node-red-stop","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":470,"y":3660,"wires":[[],[],[]]},{"id":"6f621bb4c69c4357","type":"inject","z":"0918ee609bf69fc7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":3660,"wires":[["260ff9050f0901f0"]]}]

```

---

<div class="post-metadata">

**Author:** ![wolfred](https://avatars.discourse-cdn.com/v4/letter/w/838e76/32.png) [@wolfred](https://discourse.nodered.org/u/wolfred)\
**Post date:** [16 November 2022 10:00 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/11 "2022-11-16T10:00:12Z")

</div>

I am executing NodeRed as `root` (so I don't need sudo) and this does not work ☹

What is `node-red-stop`? I cannot find anything about this command. Maybe a RPI system script to start/stop NodeRED. But not available in my Docker environment.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 November 2022 10:01 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/12 "2022-11-16T10:01:02Z")

</div>

Sorry @wolfred .... My reading skills are failing.

This is not my week for getting things right.

---

<div class="post-metadata">

**Author:** ![wolfred](https://avatars.discourse-cdn.com/v4/letter/w/838e76/32.png) [@wolfred](https://discourse.nodered.org/u/wolfred)\
**Post date:** [16 November 2022 10:01 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/13 "2022-11-16T10:01:26Z")

</div>

Solved it. There is a node `exit` to exlicity stop NodeRED: [node-red-contrib-exit (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-exit).

This works like charm.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 November 2022 10:01 UTC](https://discourse.nodered.org/t/how-to-stop-nodered-from-inside-a-flow/70693/14 "2022-11-30T10:01:34Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
