4

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

2
  • test.exe generates output and you need to do something with that output? or is test.exe your program. Commented Mar 16, 2016 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. Commented Mar 16, 2016 at 19:56

2 Answers 2

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');
});
Sign up to request clarification or add additional context in comments.

Comments

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}`);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.