I know that I can fire child processes with NodeJS and get their stdout. However, I'd like to retrieve stdout in real-time as they e because I am running a program that runs longer. Is there a way to do that in NodeJS?
This is the documentation I tried to look into: .5.8/api/child_processes.html#child_process.exec
Help? Ideas? Modules? Hacks?
I know that I can fire child processes with NodeJS and get their stdout. However, I'd like to retrieve stdout in real-time as they e because I am running a program that runs longer. Is there a way to do that in NodeJS?
This is the documentation I tried to look into: http://nodejs/docs/v0.5.8/api/child_processes.html#child_process.exec
Help? Ideas? Modules? Hacks?
Share Improve this question asked Oct 7, 2011 at 6:27 TowerTower 103k131 gold badges364 silver badges519 bronze badges 2- 3 This is a non-trivial problem because programs tend to buffer more than a single line when their stdout isn't a TTY device. This is usually solved by running the program in a pseudo-tty, but I don't know if this is possible from node.js. – Marcelo Cantos Commented Oct 7, 2011 at 6:48
- OTOH, if you don't mind lines arriving grouped in chunks, with some lines split across chunks, this won't matter to you. – Marcelo Cantos Commented Oct 7, 2011 at 6:54
2 Answers
Reset to default 14Child process stdout/stdin/stderr are Streams.
Check this page section for more information: http://nodejs/docs/latest/api/child_process.html#child_process_child_process_spawn_mand_args_options
The example on this section:
var util = require('util'),
spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
ps.stdout.on('data', function (data) {
//...
});