# Exec node - piping payload to stdin for commands

**URL:** <https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931>\
**Category:** General\
**Created:** [13 September 2024 06:00 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931 "2024-09-13T06:00:34Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [13 September 2024 12:43 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/21 "2024-09-13T12:43:47Z")

</div>

> [@dceejay](#):
>
> Yes - exactly that - with default being parameter

![Screenshot 2024-09-13 at 14.42.18](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/88b4d52b4575c01379925b21ce17ce3624793471.png)

So that's what it would look like - I still have to do the server code to support the new option.

EDIT: and also the logic to deactivate the dropdown if append isn't checked ☹

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [13 September 2024 13:15 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/22 "2024-09-13T13:15:18Z")

</div>

Ok the UI is now this:

![execwithpipe](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/7/37b0c6cd533fa06b1f86680d5be789612f47b71d.gif)

The code for the node is over at [FlowHub](https://flowhub.org/f/390d041cf14946dc) (with an explanation on how to install the node locally) with an [example flow](https://flowhub.org/f/8cfee78ee7fe6ec0) and for those interested in how I did this, [the blog post with screencast](https://blog.openmindmap.org/blog/custom-node-dev).

Btw, it's much less code doing it this way, thanks for the suggestion @dceejay!

Btw2: even if the gif seems to be different, parameter is the default value not stdin. I added an extra new parameter 'addpayTo' for storing the location.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 September 2024 13:22 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/23 "2024-09-13T13:22:22Z")

</div>

looks good - will you make it into a PR against the core node please ?

Thanks

PS - the options are usually not capitalised. So just parameter and stdin please.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [13 September 2024 13:42 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/24 "2024-09-13T13:42:25Z")

</div>

> [@dceejay](#):
>
> will you make it into a PR against the core node

I can do that, interested to know what I did wrong!

> [@dceejay](#):
>
> So just parameter and stdin please.

done.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 September 2024 13:46 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/25 "2024-09-13T13:46:12Z")

</div>

> [@gregorius](#):
>
> what I did wrong!

Nothing so far ! (as far as I can tell)

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [13 September 2024 14:32 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/26 "2024-09-13T14:32:58Z")

</div>

PR is up ==\> [Exec Node: Allow msg.payload to be redirected to stdin for the process by gorenje · Pull Request #4880 · node-red/node-red · GitHub](https://github.com/node-red/node-red/pull/4880)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [13 September 2024 17:13 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/27 "2024-09-13T17:13:42Z")

</div>

> [@jbudd](#):
>
> --- Perhaps @dceejay can suggest a work-around to trick the shell into processing a hashbang line at the start of a script passed in this way?

At the danger of being (rightly probably) told off by Dave, again. 😀

Your command needs to be python and not the shell, then you don't need the hash-bang. But you do need to then deal with passing the whole script as a string.

These threads seem to offer some ideas on the subject:

> <https://unix.stackexchange.com/questions/520357/running-multiple-line-commands-with-python>

> <https://stackoverflow.com/questions/2043453/executing-multi-line-statements-in-the-one-line-command-line>

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [13 September 2024 17:41 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/28 "2024-09-13T17:41:44Z")

</div>

There’s a million roads that lead to Rome but they all end in Rome.

Of course there are many approaches to handling scripts and doing exec outside of node red. This is just one.

I’ve put up a PR and it’s up to those who decide whether this approach fits into the concept that is node red.

I don’t much mind since node red is so flexible that I can develop my approach outside of the core of node red. And that is the great thing about node red.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [14 September 2024 04:44 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/29 "2024-09-14T04:44:15Z")

</div>

Doesn't this break the spawn mode ? and what happens to the 'extra input parameters' ?

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [14 September 2024 05:33 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/30 "2024-09-14T05:33:10Z")

</div>

Works for spawn and exec mode and the payload can _either_ be appended as input parameter _or_ streamed to stdin - it's either or, not both. By default payload is appended to the parameter _if_ append is activated. If append is _not_ activated, nothing happens with the payload.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [15 September 2024 08:40 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/31 "2024-09-15T08:40:53Z")

</div>

> [@gregorius](#):
>
> BTW what does an exec node execute if there is no command provided?

To answer this question, I had to look at the [documentation](https://github.com/node-red/node-red/blob/674eb36e15162ec62459784d023304c3fdf960b0/packages/node_modules/%40node-red/nodes/core/function/90-exec.js#L71-L78):

```auto
var arg = node.cmd;
if (node.addpay) {
    var value = RED.util.getMessageProperty(msg, node.addpay);
    if (value !== undefined) {
        arg += " " + value;
    }
}
if (node.append.trim() !== "") { arg += " " + node.append; }

```

So if `cmd` - i.e. the command is left blank - and append is activated, then the payload (in other words) becomes the command - if that's the parameter that I'm appending!

If append is not activated but there has been an append string defined, then that.

That could be documented since I didn't find that in the documentation for the exec node and I just ran into the use case _I want to defined a dynamic command line - how do I do that?_ - I had to look at the codebase to figure it out ... it wasn't described in the documentation - unfortunately.

Mea culpa: I didn't add any documentation for the feature described here 🫢

/cc @jbudd

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 December 2024 08:41 UTC](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931/32 "2024-12-14T08:41:36Z")

</div>

Closed on 14 December 2024 08:41 UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/exec-node-piping-payload-to-stdin-for-commands/90931.md?page=1)
