Skip to content

Instantly share code, notes, and snippets.

@ctavan
Created September 2, 2011 16:19
Show Gist options
  • Save ctavan/1189056 to your computer and use it in GitHub Desktop.
Save ctavan/1189056 to your computer and use it in GitHub Desktop.
Spawning a childprocess in node.js
var spawn = require('child_process').spawn;
var proc = spawn('/path/to/executable', ['param1', 'param2', 'param3']);
var self = {
_stdoutBuf: '',
_stderrBuf: ''
};
proc.stdout.on('data', function(data) {
var buf = self._stdoutBuf;
buf += data;
var newline;
while ((newline = buf.indexOf('\n')) > -1) {
var response = buf.slice(0, newline);
buf = buf.slice(newline+1);
//response = JSON.parse(response);
console.log('STDOUT: ' + response);
}
self._stdoutBuf = buf;
});
proc.stderr.on('data', function(data) {
var buf = self._stderrBuf;
buf += data;
var newline;
while ((newline = buf.indexOf('\n')) > -1) {
var response = buf.slice(0, newline);
buf = buf.slice(newline+1);
//response = JSON.parse(response);
console.log('STDERR: ' + response);
}
self._stderrBuf = buf;
});
proc.on('exit', function(code) {
console.log(' => Child process exited with code ' + code);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment