Created
September 2, 2011 16:19
-
-
Save ctavan/1189056 to your computer and use it in GitHub Desktop.
Spawning a childprocess in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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