Execute command

#1

Hello

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

thanks

#2

What type of command?

#3

actually, I did not mention…

a command cli linux

#4

Why do you not want to use the exec node?

#5

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

#6

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

#7

@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 docs on writing functions for details
  2. Read the node.js docs.on the child_process module and seek out examples.
#8

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

#9

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

#10

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

#11

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

#12

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