I am trying to make a Node.js script to analyze disk usage. I use du
for this, but it's hard for me to figure out how to read the output of the child process line by line. Here is what I have tried so far:
var spawn = require("child_process").spawn, rl = require('readline'), du = spawn('du', ['/home']); linereader = rl.createInterface(du.stdout, du.stdin); // Read line by line. //du.stdout.on('data', function (data) { linereader.on('line', function (data) { console.log(data); });
du.stdout.on('data'
just reads the pieces of data, and while readline
should apparently separate its input by line, I get the exact data instead (du.stdout returns a buffer, but calls .toString()
it gives the same data as mine with linereader
).
source share