# Daemon initiated by exec node stops with Node-red restart

**URL:** <https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106>\
**Category:** General\
**Created:** [13 September 2018 09:47 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106 "2018-09-13T09:47:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![v2c2](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@v2c2](https://discourse.nodered.org/u/v2c2)\
**Post date:** [13 September 2018 09:47 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/1 "2018-09-13T09:47:41Z")

</div>

Node-Red is on raspbian- stretch instance. OpenVpn daemon on the same instance gets initiated successfully by an exec node using the below command.

> sudo openvpn --config /etc/openvpn/client.ovpn --daemon

When working with a standard ssh terminal, this command ensures that this daemon continues to work in the background even the ssh terminal is closed after executing the command.

If node-red is stopped after executing the daemon with exec node, the daemon also stops?

I need help on how to make this execution persistent?

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [13 September 2018 09:58 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/2 "2018-09-13T09:58:43Z")

</div>

The RaspberryPi page in the docs details how to start Node-RED as a service which makes the execution persistent

[https://nodered.org/docs/hardware/raspberrypi](https://nodered.org/docs/hardware/raspberrypi)

---

<div class="post-metadata">

**Author:** ![v2c2](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@v2c2](https://discourse.nodered.org/u/v2c2)\
**Post date:** [13 September 2018 12:10 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/3 "2018-09-13T12:10:27Z")

</div>

Thank you for your reply.  
I'm sorry. I wasn't clear enough with my question.  
Nodered.service is already persistent against reboots. I need the daemon executed by the Node\_red exec node to be persistent even after a Node-Red stop. (By the way Node-Red stop is not caused by a any hardware shutdown)  
Node-Red utilizing the exec node executes the above command perfectly and openvpn daemon starts to work. No problem. However the daemon stops abruptly when node-red is stopped.

When I execute the above command from a terminal, it works. Daemon doesn't stop even if I close the terminal.

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [13 September 2018 12:33 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/4 "2018-09-13T12:33:29Z")

</div>

possible workaround: perhaps have the exec node invoke a screen session to pseud-encapsulate the launch of the daemon so it isn't dependent on node-red's shell session?

---

<div class="post-metadata">

**Author:** ![v2c2](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@v2c2](https://discourse.nodered.org/u/v2c2)\
**Post date:** [13 September 2018 12:43 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/5 "2018-09-13T12:43:06Z")

</div>

I'll try putting the command in a bash script and execute this script from exec node. Maybe that will do the trick. 👍

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [13 September 2018 15:41 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/6 "2018-09-13T15:41:54Z")

</div>

Are you using "spawn" mode in your `exec` node? (not sure if that will help, however)

Also, I've not tried it, but there is another related node in the flows library, called [node-red-node-daemon](https://flows.nodered.org/node/node-red-node-daemon) -- although I would think that a node-red server restart would still lose its connection to any process it started...

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 September 2018 17:12 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/7 "2018-09-13T17:12:56Z")

</div>

when the Node-RED exit it closes the flow - which in this case (and for the daemon node) calls kill on the child process. If you need it to run independent of Node-RED you may be able to call it via another shell command (as suggested above) - or just start it outside of Node-RED altogether.

---

<div class="post-metadata">

**Author:** ![v2c2](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@v2c2](https://discourse.nodered.org/u/v2c2)\
**Post date:** [14 September 2018 12:00 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/8 "2018-09-14T12:00:29Z")

</div>

@shrickus I'm not using "spawn" mode. I tried "node-red-node-daemon" but it didn't help.

@dceejay I tried the bash script option (put the command in a bash script and execute it via exec node). I also tried using "nohub command &" to detach the process. But these didn't work out. I'll find another way to start it outside of Node-RED as you suggested.

Thanks everyone for your replies.

---

<div class="post-metadata">

**Author:** ![moebigit](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@moebigit](https://discourse.nodered.org/u/moebigit)\
**Post date:** [21 August 2019 02:16 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/9 "2019-08-21T02:16:56Z")

</div>

I am having the exact same use case and I can’t find a way to get it to work other than maybe scheduling the command via crontab.

The exec command shall execute a script that shuts down nodered to free the serial connection, then flashes the Arduino and then starts nodered as service again.

I tried nohup and & for the commands inside the script and to start the script itself but nothing helps to have the process running in its own parent process.

---

<div class="post-metadata">

**Author:** ![AlonAmiraabluedragon](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alonamiraabluedragon/32/24155_2.png) [@AlonAmiraabluedragon](https://discourse.nodered.org/u/AlonAmiraabluedragon)\
**Post date:** [21 June 2020 09:39 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/10 "2020-06-21T09:39:42Z")

</div>

Here's what I've used to start a process detach & disowned (on linux).

1. Put child\_process in your globalContext

2. Put the following in a function node:

```auto
const child = global.get('child_process').exec(`
bash << EOF
(
node yourScript.js </dev/null &>/dev/null &
disown
)
EOF
`);
child.unref();
return msg;

```

---

<div class="post-metadata">

**Author:** ![hepcat72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hepcat72/32/8078_2.png) [@hepcat72](https://discourse.nodered.org/u/hepcat72)\
**Post date:** [21 March 2021 15:25 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/11 "2021-03-21T15:25:16Z")

</div>

So if you wanted to shell something out, you would put the system call inside that code? Am I understanding that correctly?

In my case, I'm making a system call to VLC to record a show from my HDHR, but I discovered that if I redeploy during the recording, the process gets killed and I just want to avoid that.

I'm surprised nohup doesn't work.

---

<div class="post-metadata">

**Author:** ![hepcat72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hepcat72/32/8078_2.png) [@hepcat72](https://discourse.nodered.org/u/hepcat72)\
**Post date:** [9 August 2022 17:41 UTC](https://discourse.nodered.org/t/daemon-initiated-by-exec-node-stops-with-node-red-restart/3106/12 "2022-08-09T17:41:10Z")

</div>

Has anyone created a node for this for system calls?
