# How to execute a commandline with Node-red

**URL:** <https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961>\
**Category:** General\
**Created:** [31 March 2020 10:03 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961 "2020-03-31T10:03:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpap](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dpap/32/18695_2.png) [@dpap](https://discourse.nodered.org/u/dpap)\
**Post date:** [31 March 2020 10:03 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/1 "2020-03-31T10:03:31Z")

</div>

Hi,  
I've read a lot of posts about my question, but haven't found the solution yet.  
I am very new to the Node-Red and Js domain and am trying to do a simple project.  
I found an example of uploading a file with the dashboard that suits me but I also want to execute a command for the mosquitto server to publish the file, that is, to execute the command  
"mosquitto\_pub -h 192.168.2.100 -p 1883 -u auser -P apassword -t uploads/192.168.2.101 -f /tmp/build\*.tmp/afile.bin".  
I tried a node exec but I failed.  
I understand it's a very simple matter for someone who knows a little bit, but I'm not one of them. If anyone wants to help me I would appreciate it. Thanks

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 March 2020 10:16 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/2 "2020-03-31T10:16:39Z")

</div>

Rather than executing mosquitto\_pub (which you could do with an Exec node, but then your user and pwd would exist in plain text in the flow, which is not good) have a look at the MQTT nodes which allow you to subscribe and publish to mqtt brokers such as mosquitto.  
I think they are installed as part of the core nodes should already be there in the palette.

**[edit]** Actually I am not sure whether you can publish a file like that with the MQTT nodes, someone else may perhaps comment on that.  
What was the problem with the exec node? Look on all the outputs of the exec node to see the error. You might need to provide the full path to mosquitto\_sub (somwthing like `/usr/local/bin/mosquitto_sub`, depending on your OS.

---

<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:** [31 March 2020 10:29 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/3 "2020-03-31T10:29:43Z")

</div>

@colin - MQTT just transfers bytes from one place to another so yes you can send files... use a file-in to read the file - set it to deliver a buffer - and feed straight into the mqtt out node - (and set that with the appropriate topic, user, password, etc)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 March 2020 12:38 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/4 "2020-03-31T12:38:28Z")

</div>

Thanks, that what I thought, but hadn't seen the -f option with mosquitto\_sub before, so thought I should better not pronounce lest I was wrong.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [31 March 2020 12:47 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/5 "2020-03-31T12:47:31Z")

</div>

The `-f` option wouks fine in an `exec` node, you just have to provide the correct path to the file.

---

<div class="post-metadata">

**Author:** ![dpap](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dpap/32/18695_2.png) [@dpap](https://discourse.nodered.org/u/dpap)\
**Post date:** [31 March 2020 12:58 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/6 "2020-03-31T12:58:58Z")

</div>

thanks zenofmud for your help.  
I tried your suggestion with the node file-in and it works but with a fixed file name.  
Could not find how to set the filename to  
_**$env('UPLOAD\_DIR') & '/' & req.files[0].originalname**_  
(from a change-node's output) that I want

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [31 March 2020 14:36 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/7 "2020-03-31T14:36:18Z")

</div>

> [@dpap](#):
>
> I tried your suggestion with the node file-in and it works but with a fixed file name.

Um, Dave offered that suggestion 😁  
So where/how are you setting _**$env('UPLOAD\_DIR')**_?  
where is _**req.files[0].originalname**_ coming from?  
What platform and OS are you on?

---

<div class="post-metadata">

**Author:** ![dpap](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dpap/32/18695_2.png) [@dpap](https://discourse.nodered.org/u/dpap)\
**Post date:** [31 March 2020 15:07 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/8 "2020-03-31T15:07:02Z")

</div>

Sorry Dave!  
_**$env('UPLOAD\_DIR') & '/' & req.files[0].originalname**_ is the msg.payload of a change-node that takes the file I choose, and the OS is raspberryPi's OS (rasbian lite).  
The file-in node takes a fixed filename not a variable as I want. How can I force the file-in node to get a variable filename ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 March 2020 15:25 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/9 "2020-03-31T15:25:20Z")

</div>

Look in the info tab for the node and you will see that you can pass the filename in msg.filename.

---

<div class="post-metadata">

**Author:** ![dpap](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dpap/32/18695_2.png) [@dpap](https://discourse.nodered.org/u/dpap)\
**Post date:** [31 March 2020 20:50 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/10 "2020-03-31T20:50:19Z")

</div>

I'm sorry but i'm stupid.  
I can't see where can insert "msg.filename".  
This is the screen hardcopy i have openning info tab

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cda770e9a8a0e6e5fbc7f826cc4c1a3b42ddb47f.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 March 2020 20:54 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/11 "2020-03-31T20:54:24Z")

</div>

To tell the node to read the file you have to send it a message, in that message you need to set msg.filename to the required name.  
In the node config screen itself (when you double click the node to open it) leave the filename field empty.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [31 March 2020 20:56 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/12 "2020-03-31T20:56:53Z")

</div>

When it says "if not set in the node configuration" it means if you haven't filled in (set) a fiename.

---

<div class="post-metadata">

**Author:** ![dpap](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dpap/32/18695_2.png) [@dpap](https://discourse.nodered.org/u/dpap)\
**Post date:** [31 March 2020 22:32 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/13 "2020-03-31T22:32:59Z")

</div>

Many thanks Colin, now it works as i needed!

---

<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:** [30 May 2020 22:33 UTC](https://discourse.nodered.org/t/how-to-execute-a-commandline-with-node-red/23961/14 "2020-05-30T22:33:06Z")

</div>

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