Exec

Node-RED is written in Javascript, as are the custom nodes in the Flows Library. At times it would be great to use programs written in other languages. In this guide we'll explain the exec node.

Exec Node

Node-RED by default comes with the exec node. This node allows you to run a command as if you're on the command line. The exec node has one input, and three outputs. Let's create a basic example and work from there. Let's connect the inject to the exec input, and connect the three dots to debug nodes. I've set the command in the exec node to date.

"Wired up exec node"

When triggered, you'll see two outputs in the debug pane. One from Standard out; the date and timestamp of the execution and the Exit code will output too. Standard error didn't output anything.

Let's explain what happened: the date command was executed by the NodeJS without any input. The command returned the date as a string, and execution of the commmand completed. There weren't any errors, so the error code is set to zero. Non-zero exit codes imply didn't go as intended. Output is written to the standard output, and not standard error. This allows us to handle errors differently from succesful exections and the output in standard output.

Exec vs Spawn mode

Let's change the exec node to execute ping google.com. The ping command provides the round-trip time to google.com in milliseconds, each second. When we deploy, the program is stuck. That's because the output is generated each second and won't complete until there's a failure in the command. Not what you'd want.

When there's continous output you'll need to select spawn mode in the settings windown of the exec node:

spawn mode for exec in Node-RED

This will produce a message per line to the standard out output for further processing.

Shell Expansions

As the arguments for the exec nodes are executed as is, shell expansions will not work. For example when the exec command is rm -r /path/to/dir/*, the * will not be expanded and Node-RED will try to remove the file or directory called * in the /path/to/dir directory.

FlowFuse

On FlowFuse Cloud, the Node-RED exec nodes are disabled. The Stacks, that is the containers that Node-RED runs in, don't include any accessible executatables so there would be no benefit to running exec commands. All the data is exposed through Javascript and Node.JS already.

Node Documentation

Runs a system command and returns its output.

The node can be configured to either wait until the command completes, or to send its output as the command generates it.

The command that is run can be configured in the node or provided by the received message.

Inputs

payload string
if configured to do so, will be appended to the executed command.
kill string
the type of kill signal to send an existing exec node process.
pid number|string
the process ID of an existing exec node process to kill.

Outputs

  1. Standard output
    payload string
    the standard output of the command.
    rc object
    exec mode only, a copy of the return code object (also available on port 3)
  2. Standard error
    payload string
    the standard error of the command.
    rc object
    exec mode only, a copy of the return code object (also available on port 3)
  3. Return code
    payload object
    an object containing the return code, and possibly message, signal properties.

Details

By default uses the exec system call which calls the command, waits for it to complete, and then returns the output. For example a successful command should have a return code of { code: 0 }.

Optionally can use spawn instead, which returns the output from stdout and stderr as the command runs, usually one line at a time. On completion it then returns an object on the 3rd port. For example, a successful command should return { code: 0 }.

Errors may return extra information on the 3rd port msg.payload, such as a message string, signal string.

The command that is run is defined within the node, with an option to append msg.payload and a further set of parameters.

Commands or parameters with spaces should be enclosed in quotes - "This is a single parameter"

The returned payload is usually a string, unless non-UTF8 characters are detected, in which case it is a buffer.

The node's status icon and PID will be visible while the node is active. Changes to this can be read by the Status node.

The Hide console option will hide the process console normally shown on Windows systems.

Killing processes

Sending msg.kill will kill a single active process. msg.kill should be a string containing the type of signal to be sent, for example, SIGINT, SIGQUIT or SIGHUP. Defaults to SIGTERM if set to an empty string.

If the node has more than one process running then msg.pid must also be set with the value of the PID to be killed.

If a value is provided in the Timeout field then, if the process has not completed when the specified number of seconds has elapsed, the process will be killed automatically

Tip: if running a Python app you may need to use the -u parameter to stop the output being buffered.