Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I check stdout in NodeJS in real-time?

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 come 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.org/docs/v0.5.8/api/child_processes.html#child_process.exec

Help? Ideas? Modules? Hacks?

like image 280
Tower Avatar asked Dec 09 '22 05:12

Tower


1 Answers

Child process stdout/stdin/stderr are Streams.

Check this page section for more information: http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_spawn_command_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);
});
like image 117
dresende Avatar answered Dec 29 '22 21:12

dresende