Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from a stdout in real time using node.js

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

like image 845
Душан Мијаиловић Avatar asked Nov 16 '25 01:11

Душан Мијаиловић


1 Answers

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');
});
like image 89
mscdex Avatar answered Nov 17 '25 19:11

mscdex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!