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 plus 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

Running Python Scripts

If the script is running continuously and you need to see the output in a debug node or use it in the flow then you need to use the -u option to flush the output buffer.

python -y myscript.py

See here

Video

Flow used in Tutorial

download



Related Tutorials

Click to rate this post!
[Total: 2 Average: 3.5]

21 comments

  1. hello Steve,

    I had a quick question, I haven’t used the Exec Node Run External Command yet and I have little experience working with Node Red so I am still getting a hang of it, but it looks promising for a project I am working on, my question is, is there a way to read in a output from the terminal and use the output as a msg. for a flow in node Red?

    like for a simple example, I call a math script in the terminal using Exec and it produce a result, how could I used said result in node Red?

  2. Steve,
    I want to put a Dashboard Button on my clients user interface to automatically go to a google drive to download and incorporate new flows. Can I do this with an Exec node? If so, can you give me some guidance on the flow/code needed for this? I am a novice on node red.
    Thanks,
    Dave

    1. If you share the files sho anyone with a link can access them then use the http request node to get them.
      Here is a quick demo flow.
      [
      {
      “id”: “8f0fd9c1b059cce8”,
      “type”: “inject”,
      “z”: “ae06e823b34ab016”,
      “name”: “”,
      “props”: [
      {
      “p”: “payload”
      },
      {
      “p”: “topic”,
      “vt”: “str”
      }
      ],
      “repeat”: “”,
      “crontab”: “”,
      “once”: false,
      “onceDelay”: 0.1,
      “topic”: “”,
      “payload”: “”,
      “payloadType”: “date”,
      “x”: 120,
      “y”: 320,
      “wires”: [
      [
      “7fcd6b8734c36f5e”
      ]
      ]
      },
      {
      “id”: “7fcd6b8734c36f5e”,
      “type”: “http request”,
      “z”: “ae06e823b34ab016”,
      “name”: “”,
      “method”: “GET”,
      “ret”: “txt”,
      “paytoqs”: “ignore”,
      “url”: “https://www.google.com/maps/d/edit?mid=1XXs67V8tSNrYl1Z6f1bEb39h4UI&usp=share_link”,
      “tls”: “”,
      “persist”: false,
      “proxy”: “”,
      “authType”: “”,
      “senderr”: false,
      “headers”: [],
      “x”: 390,
      “y”: 400,
      “wires”: [
      [
      “a2961262f14c78a5”
      ]
      ]
      },
      {
      “id”: “a2961262f14c78a5”,
      “type”: “debug”,
      “z”: “ae06e823b34ab016”,
      “name”: “debug 67”,
      “active”: true,
      “tosidebar”: true,
      “console”: false,
      “tostatus”: false,
      “complete”: “true”,
      “targetType”: “full”,
      “statusVal”: “”,
      “statusType”: “auto”,
      “x”: 520,
      “y”: 220,
      “wires”: []
      }
      ]

      1. Steve,
        Thanks for the quick reply!
        I tried importing the above flow and it gave me all kinds of Syntax errors. I used the copy and paste into the Node Red import function. Should I be using a different technique to get the flows into Node Red?
        Dave

        1. Not sure what you mean by that. Do you want to start a command and read the output or is it a python command that is running in a console?
          rgds
          Steve

  3. Hi Steve,
    thank you very much for all your great work on node-RED, MQTT and IoT.
    A lot of your examples I have been able to reproduce at my end and I was able to learned quite a bit.
    I’m running Node-RED v2.1.3 on a Raspi 4B with 8G Memory and an SSD as a server on Debian buster.
    When trying to run the program mc with the exec note from a testMc.sh file with the proper execution rights or direct within the exec note I get the error TERM variable is not set in the debug note and an error 1 in the exec note.
    It looks to me that I need to set the TERM variable within node-RED, however I can see the variable in a listing with the cli command export
    declare -x TERM=”xterm-256color”
    Do you have by any chance, an example to get this type of issue resolved or can you point me into the right direction?
    Thank you very much for any help.

    1. Maybe you need to set the environment variable in node-red . Use the ask steve page so I can use email and then you can send me a screen shot and I will take a look.
      Rgds
      Steve

      1. Hello Steve,
        thank you for feedback and the offer to help, sorry for the delayed response. Unfortunately, I only noticed your response just now.
        Your point with the environment seems to be the issue with Linux, since I had no problem to run Exec Nodes with Node-RED in Windows 10.
        In any case, I will try to find a solution, but would be glad to take your offer, in case I canĀ“t resolve the issue myself. Thanks again for all your fantastic work and support.
        Happy New Year and have a successful and healthy 2023.

  4. Becaus of a ” |grep -v command” I put my command to a script. But now I have the Problem that the script output do not reach node red.

    What could I do here?

    Nodered shows me a pid from the bash script but the inside command seems get another pid. Is that the cause?

      1. #!/bin/bash
        export SYSTEMD_COLORS=false
        journalctl -t paperless-scan -o cat -f -n1 -p5|grep -v “Reading data”

        The overall goal I have is to get a continuous output of logs on the Dashboard.
        Some of my scripts put data into journalctl e.g. :
        echo “Test” | systemc-cat systemd-cat -t paperless-scan -p5

        on NodeRed I use as command the script /path/script.sh
        and have the Spawn setting active.

        If i run the script on shell I get the correct output. But not on nodered.

    1. Hi
      I’m seeing images. Firefox have a reader view mode which removes images maybe it has been toggled. It is to th right of the url field and looks like a text icon.
      rgds
      steve

Leave a Reply to rondo Cancel reply

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