The exec node allows you to take any existing system command, python program or script that you have written and run it from node-red and incorporate the results in you flow.
The exec node takes a single input and has three outputs.
- Output 0= standard output=output+Return Code
- Output 1= standard error=Error +Return Code
- output 2= Return code
The standard output under normal operation returns the command output + the return code which should be 0 for a successful operation.
You can see in the screen shot below the result of running the ping command. You see the payload contains the command results and the rc object contains the return code which is 0 indicating success.
Exec Node Configuration Properties
Entering the Command
The command is usually entered into the node and the input is used to trigger the node, and pass in optional parameters.
However you can pass in the complete command using the input node.
Alternatively you can pass in additional options using the input node.
The following flows illustrate the options.
Running the Command – Exec or Spawn
You have two options when running the command the default is exec.
In this mode nothing is returned until the command finishes.
Running the ping command, for example, when we run two pings then the results of both pings are returned in the payload.
You can see this in the screen shot below.
If we run the same command but use SPAWN then we see the results of each ping returned as separate messages.
When running the command the node shows the PID (process id) as the status and when finished it shows the rc code.
You can use the status node to capture the process id, and even kill the process.
I’ve modified the flow to include the status node and this is what is displays when running.
Errors can be detected by examining the return code which is available on all outputs.
Any return code above 0 is an error. Below is a screen shot showing the result of an error. The code returned is 127.
Killing a Process
The node documentation mentions that you can pass in various signals to kill the process or maybe as in the case of mosquitto the SIGHUP command which will cause it to reload the configuration file.
The screen shot below shows the flow to start, stop the mosquitto broker and to reload the config file using node-red.
Note to see the node red console I has to use the 2 output.
Below is the configuration
The inject nodes inject the strings SIGHUP or SIGTERM because the exec node needs these to be in msg.kill object I use a change node to transpose the payload object to the kill object. You can see this in the screen shot below.
Flow used in Tutorial