3

I have a problem, I need to read from a console output in real time. I have a file that I need to execute, tried doing something like this test.exe > text.txt but when I try to read while exe file is running I can't see anything until exe finishes and write all lines in the same time. I need to do this using node.js

  • test.exe generates output and you need to do something with that output? or is test.exe your program. – skarface Mar 16 '16 at 19:51
  • test.exe is a program that I start and it generates output, I tried redirecting it to file but I can't read anything from file until test.exe finishes. It goes something like this `execFile('test.exe > test.txt',function(err, stdout, stderr) { console.log(stdout); }).on('data', function (stdout){ sendResponse(); console.log(stdout.toString() + "stdout"); });` I need to read from test.txt while this is running and send it as a response to post request from client side. I need to do something like progress bar for that test.exe program. – Душан Мијаиловић Mar 16 '16 at 19:56

2 Answers2

5

You should be able to use child_process.spawn() to start the process and read from its stdout/stderr streams:

var spawn = require('child_process').spawn;
var proc = spawn('test.exe');
proc.stdout.on('data', function(data) {
  process.stdout.write(data);
});
proc.stderr.on('data', function(data) {
  process.stderr.write(data);
});
proc.on('close', function(code, signal) {
  console.log('test.exe closed');
});
mscdex
  • 104,356
  • 15
  • 192
  • 153
1

test.exe probably buffers it's output.

You can try to run it with spawn, or with a pseudo tty

const spawn = require('child_process').spawn;
const type = spawn('type.exe');

type.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

type.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

type.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});
Community
  • 1
  • 1
bolav
  • 6,938
  • 2
  • 18
  • 42