I'm trying to capture the output from a child process and send that through a websocket but I'm having problems capturing the on.(data, callback)
event.
var subscriberProc = spawn('./stdoutsub',
['mytopic', '--clientid', 'node-dispatcher'],
{cwd: '/Users/oskar/projects/others/rsmb_1/mac/'});
subscriberProc.stdout.on('data', function(data) {
console.log('-> "' + data + '"');
wsServer.send(data);
});
If I change to the following, the stdoutsub process prints to the node process stdout with the expected data.
var subscriberProc = spawn('./stdoutsub',
['mytopic', '--clientid', 'node-dispatcher'],
{cwd: '/Users/oskar/projects/others/rsmb_1/mac/',
stdio: 'inherit'});
I'm running node version 0.8.1 on OSX.
Anything I'm missing here?
I have the same problem. I am guessing the problem might be that spawn starts the command before stdout stream has that data event handler and because of that it doesn't get the data. At the moment I haven't figured out any way to setup the stream first to test my theory...
I was having similar issue (where I was missing the output in certain cases) and concluded that the spawned process is spewing output before I get a chance to start listening for it.
I solved it by using code similar to below from Node.js documentation :
var fs = require('fs'),
spawn = require('child_process').spawn,
out = fs.openSync('./out.log', 'a'),
err = fs.openSync('./out.log', 'a');
var child = spawn('prg', [], {
detached: true,
stdio: [ 'ignore', out, err ]
});
And then I read out.log and so far things have worked out fine for me.