最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I check stdout in NodeJS in real-time? - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 14

Child 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) {
   //...
});
发布评论

评论列表(0)

  1. 暂无评论