diff --git a/nodes/core/core/75-exec.html b/nodes/core/core/75-exec.html index 2336c021b..8123163c2 100644 --- a/nodes/core/core/75-exec.html +++ b/nodes/core/core/75-exec.html @@ -32,6 +32,7 @@ +
Tip: spawn expects only one command word - and appended args to be comma separated.
diff --git a/nodes/core/core/75-exec.js b/nodes/core/core/75-exec.js index dd806df6a..eb6834e49 100644 --- a/nodes/core/core/75-exec.js +++ b/nodes/core/core/75-exec.js @@ -21,8 +21,8 @@ var exec = require('child_process').exec; function ExecNode(n) { RED.nodes.createNode(this,n); - this.cmd = n.command; - this.append = n.append || ""; + this.cmd = n.command.trim(); + this.append = n.append.trim() || ""; this.useSpawn = n.useSpawn; var node = this; @@ -32,25 +32,30 @@ function ExecNode(n) { if (this.useSpawn == true) { // make the extra args into an array // then prepend with the msg.payload - var arg = node.append.split(","); - if (msg.payload != " ") { arg.unshift(msg.payload); } - node.log(node.cmd+" "+arg); - var ex = spawn(node.cmd,arg); - ex.stdout.on('data', function (data) { - //console.log('[exec] stdout: ' + data); - msg.payload = data; - node.send([msg,null,null]); - }); - ex.stderr.on('data', function (data) { - //console.log('[exec] stderr: ' + data); - msg.payload = data; - node.send([null,msg,null]); - }); - ex.on('close', function (code) { - //console.log('[exec] result: ' + code); - msg.payload = code; - node.send([null,null,msg]); - }); + if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); } + var arg = []; + if (node.append.length > 0) { arg = node.append.split(","); } + if (msg.payload.trim() != "") { arg.unshift(msg.payload); } + node.log(node.cmd+" ["+arg+"]"); + if (node.cmd.indexOf(" ") == -1) { + var ex = spawn(node.cmd,arg); + ex.stdout.on('data', function (data) { + //console.log('[exec] stdout: ' + data); + msg.payload = data.toString(); + node.send([msg,null,null]); + }); + ex.stderr.on('data', function (data) { + //console.log('[exec] stderr: ' + data); + msg.payload = data.toString(); + node.send([null,msg,null]); + }); + ex.on('close', function (code) { + //console.log('[exec] result: ' + code); + msg.payload = code; + node.send([null,null,msg]); + }); + } + else { node.error("Spawn command must be just the command - no spaces or extra parameters"); } } else {