1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

exec node returns 0 on the third output if command ended without error. (#1160)

* exec node returns 0 on the third output if command ended without error.
Otherwise, the status of the node is updated and the error code is send through the third output.

* info text updated and the second output returns only something if stderr is not empty

* proper stderror handling

* proper handling of stderr
This commit is contained in:
cinhcet 2017-02-22 23:22:06 +01:00 committed by Dave Conway-Jones
parent a625eeeac8
commit ee0bd49918
2 changed files with 12 additions and 5 deletions

View File

@ -47,10 +47,13 @@
<p>Calls out to a system command.<br/></p> <p>Calls out to a system command.<br/></p>
<p>Provides 3 outputs: stdout, stderr, and return code.</p> <p>Provides 3 outputs: stdout, stderr, and return code.</p>
<p>By default uses the <code>exec</code> system call which calls the command, then gets a callback <p>By default uses the <code>exec</code> system call which calls the command, then gets a callback
on completion, returning the complete result in one message, along with any errors.</p> on completion, returning stdout as the payload to the first, the error code as the third
and, if available, stderr to the second output. If no error occurred, a zero is returned on the third output.</p>
<p>Optionally can use <code>spawn</code> instead, which returns the output from stdout and stderr <p>Optionally can use <code>spawn</code> instead, which returns the output from stdout and stderr
as the command runs (usually one line at a time). On completion it then returns a return code as the command runs (usually one line at a time). On completion it then returns a return code
(on the 3rd output).</p> (on the 3rd output).</p>
<p>The <code>exec</code> method spawns a subshell and therefore can be used for more complicated
commands involving pipes. However, it waits for completion of the whole command before returing anything.</p>
<p>The optional append gets added to the command after <code>msg.payload</code> - so you can do <p>The optional append gets added to the command after <code>msg.payload</code> - so you can do
things like pipe the result to another command.</p> things like pipe the result to another command.</p>
<p>Commands or parameters with spaces should be enclosed in quotes - <i>"This is a single parameter"</i></p> <p>Commands or parameters with spaces should be enclosed in quotes - <i>"This is a single parameter"</i></p>

View File

@ -102,15 +102,19 @@ module.exports = function(RED) {
child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) { child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) {
msg.payload = new Buffer(stdout,"binary"); msg.payload = new Buffer(stdout,"binary");
if (isUtf8(msg.payload)) { msg.payload = msg.payload.toString(); } if (isUtf8(msg.payload)) { msg.payload = msg.payload.toString(); }
var msg2 = {payload:stderr}; var msg2 = null;
var msg3 = null; if(stderr) {
msg2 = {payload: stderr};
}
var msg3 = {payload:0};
node.status({});
//console.log('[exec] stdout: ' + stdout); //console.log('[exec] stdout: ' + stdout);
//console.log('[exec] stderr: ' + stderr); //console.log('[exec] stderr: ' + stderr);
if (error !== null) { if (error !== null) {
msg3 = {payload:error}; msg3 = {payload:error.code};
node.status({fill:"red",shape:"dot",text:"error: "+error.code});
//console.log('[exec] error: ' + error); //console.log('[exec] error: ' + error);
} }
node.status({});
node.send([msg,msg2,msg3]); node.send([msg,msg2,msg3]);
if (child.tout) { clearTimeout(child.tout); } if (child.tout) { clearTimeout(child.tout); }
delete node.activeProcesses[child.pid]; delete node.activeProcesses[child.pid];