Daemon initiated by exec node stops with Node-red restart

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?

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


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.

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?

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

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 -- although I would think that a node-red server restart would still lose its connection to any process it started...

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.

@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.

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.

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:

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

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.

Has anyone created a node for this for system calls?