# Execute command

**URL:** <https://discourse.nodered.org/t/execute-command/5122>\
**Category:** General\
**Created:** [22 November 2018 14:54 UTC](https://discourse.nodered.org/t/execute-command/5122 "2018-11-22T14:54:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![baudetd](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@baudetd](https://discourse.nodered.org/u/baudetd)\
**Post date:** [22 November 2018 14:54 UTC](https://discourse.nodered.org/t/execute-command/5122/1 "2018-11-22T14:54:50Z")

</div>

Hello

how to execute a command without using the exec node but in a function ?  
and recover the value

thanks

---

<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:** [22 November 2018 15:32 UTC](https://discourse.nodered.org/t/execute-command/5122/2 "2018-11-22T15:32:33Z")

</div>

What type of command?

---

<div class="post-metadata">

**Author:** ![baudetd](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@baudetd](https://discourse.nodered.org/u/baudetd)\
**Post date:** [22 November 2018 16:22 UTC](https://discourse.nodered.org/t/execute-command/5122/3 "2018-11-22T16:22:07Z")

</div>

actually, I did not mention…

a command cli linux

---

<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:** [22 November 2018 16:29 UTC](https://discourse.nodered.org/t/execute-command/5122/4 "2018-11-22T16:29:01Z")

</div>

Why do you not want to use the `exec` node?

---

<div class="post-metadata">

**Author:** ![baudetd](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@baudetd](https://discourse.nodered.org/u/baudetd)\
**Post date:** [22 November 2018 17:40 UTC](https://discourse.nodered.org/t/execute-command/5122/5 "2018-11-22T17:40:03Z")

</div>

because I want to execute commands before others, I prefer to do it in my function. my program will be cleaner

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [22 November 2018 18:16 UTC](https://discourse.nodered.org/t/execute-command/5122/6 "2018-11-22T18:16:10Z")

</div>

You could do it in a python script search on the flows site for one of the python nodes.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 November 2018 19:20 UTC](https://discourse.nodered.org/t/execute-command/5122/7 "2018-11-22T19:20:03Z")

</div>

@baudetd to do it in your function node you would need to use the `child_process` module from the node.js runtime.

You will need to:

1. Use the `functionGlobalContext` setting to make the module available to the function node. See the [nodered.org](http://nodered.org) docs on writing functions for details
2. Read the node.js docs.on the child\_process module and seek out examples.

---

<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:** [22 November 2018 23:52 UTC](https://discourse.nodered.org/t/execute-command/5122/8 "2018-11-22T23:52:01Z")

</div>

Which is of course just what the exec node does for you....

---

<div class="post-metadata">

**Author:** ![baudetd](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@baudetd](https://discourse.nodered.org/u/baudetd)\
**Post date:** [25 November 2018 14:59 UTC](https://discourse.nodered.org/t/execute-command/5122/9 "2018-11-25T14:59:40Z")

</div>

> [@knolleary](#):
>
> `functionGlobalContext`

const { spawn } = require('child\_process');  
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) =\> {  
msg.payload = msg.payload + (`stdout: ${data}`);  
});

ls.stderr.on('data', (data) =\> {  
msg.payload = msg.payload +(`stderr: ${data}`);  
});

ls.on('close', (code) =\> {  
msg.payload = msg.payload +(`child process exited with code ${code}`);  
});

but i have an error with " `functionGlobalContext`"  
i Don't understand that

"ReferenceError: require is not defined (line 0, col 84)"

finally

I would like to run a omxplayer command and be able to kill it in a function  
thanks for your help

---

<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:** [25 November 2018 15:42 UTC](https://discourse.nodered.org/t/execute-command/5122/10 "2018-11-25T15:42:20Z")

</div>

or use the pre-built node ? [https://flows.nodered.org/node/node-red-contrib-pi-omxplayer](https://flows.nodered.org/node/node-red-contrib-pi-omxplayer)

---

<div class="post-metadata">

**Author:** ![baudetd](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@baudetd](https://discourse.nodered.org/u/baudetd)\
**Post date:** [25 November 2018 16:40 UTC](https://discourse.nodered.org/t/execute-command/5122/11 "2018-11-25T16:40:01Z")

</div>

no , i know this but i Don't use the prebuilt node

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [25 November 2018 18:12 UTC](https://discourse.nodered.org/t/execute-command/5122/12 "2018-11-25T18:12:54Z")

</div>

As I said, you need to read the documentation on writing functions to learn how to require additional modules. You cannot require them directly.

---

<div class="post-metadata">

**Author:** ![krzydec](https://avatars.discourse-cdn.com/v4/letter/k/a5b964/32.png) [@krzydec](https://discourse.nodered.org/u/krzydec)\
**Post date:** [16 February 2022 08:03 UTC](https://discourse.nodered.org/t/execute-command/5122/13 "2022-02-16T08:03:06Z")

</div>

Hello

If anyone will find this thread wondering if the shell command can be executed in the function node, I'm posting a quick guide:

1. Import _child\_process_ module to your function node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/d/7d6416081d7fd13e496e46f11e5d152944caf42a.png)

1. write your code 😉

```auto
//documentation:
//https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback
msg1={};
var child_process = childProcess.exec('echo Hello World!',(error, stdout, stderr) => {
  if (error) {
    node.warn(`exec error: ${error}`);
    return;
  }
  node.warn(`stdout: ${stdout}`);
  /*
  //^^^new message can be send out instead of node.warn()^^^//
  msg1.stdout = `${stdout}`;
  node.send(msg1);
  */
  node.warn(`stderr: ${stderr}`);
});
return msg1;

```

example flow:

```auto
[{"id":"ec878471ef7862aa","type":"inject","z":"866282f4.d0922","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":470,"y":1700,"wires":[["51ba29de9398140e"]]},{"id":"51ba29de9398140e","type":"function","z":"866282f4.d0922","name":"childProcess exec","func":"//documentation:\n//https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback\nmsg1={};\nvar child_process = childProcess.exec('echo Hello World!',(error, stdout, stderr) => {\n if (error) {\n node.warn(`exec error: ${error}`);\n return;\n }\n node.warn(`stdout: ${stdout}`);\n /*\n //^^^new message can be send out instead of node.warn()^^^//\n msg1.stdout = `${stdout}`;\n node.send(msg1);\n */\n node.warn(`stderr: ${stderr}`);\n});\nreturn msg1;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"childProcess","module":"child_process"}],"x":710,"y":1700,"wires":[["dde19cfc495b1608"]]},{"id":"dde19cfc495b1608","type":"debug","z":"866282f4.d0922","name":"childProcess exec","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":1700,"wires":[]}]

```
