Using the Exec Node to Run External Commands

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.node-red-exec-node



  • 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-output

Exec Node Configuration Properties

exec-node-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.exec-node-examples

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.

exec-mode-run

If we run the same command but use SPAWN then we see the results of each ping returned as separate messages.

exec-node-using-Spawn

Node Status

When running the command the node shows the PID (process id) as the status and when finished it shows the rc code.

exec-node-status

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.

exec-node-status-pid

Errors

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.

node-red-exec-error

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.

run_mosquitto_exec_node

Note to see the node red console I has to use the 2 output.

Below is the configuration

exec-node-mosquitto-config

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.

exec-node-2

Resources:

Video- How to Run System Commands In Node-Red

Flow used in Tutorial

Related Tutorials

Please rate? And use Comments to let me know more
[Total: 1   Average: 3/5]

Leave a Reply

Your email address will not be published. Required fields are marked *