node.js - Nodejs read stdout of some process and do stuff -


i want read stdout of process nodejs, in windows.

so far got this:

i simulate process output :

ping -t google.com > ping.txt 

the ping output dumped in ping.txt file. file watched in node changes.

so, in nodejs got code:

var filename = 'ping.txt';  var fs = require("fs");  fs.watch(filename, function (event, filename) {     if (event == 'change') {         console.log('change', filename);          fs.stat(filename, function (err, stats) {             console.log('size ', stats.size);              if(stats.size > 500){                                 fs.truncate(filename,10,function(){                     console.log("truncated");                 });              }                     })     } }); 

but nodejs output:

size  0 change ping.txt size  192 change ping.txt size  253 change ping.txt size  375 change ping.txt size  436 change ping.txt size  559 truncated change ping.txt size  620 truncated change ping.txt size  743 truncated change ping.txt size  804 truncated change ping.txt size  926 truncated 

the file never gets truncated.

i don't want massive file size, because real process getting lot of output.

so that's main reason try file truncated.

but it's not working.

can give me hand? first nodejs experiment.

later i'm planning output stdout process trought websocket, i'm stucked problem.

thanks in advance ! best regards!

edit 1: ping process not real 1 i'm trying read. real process cryptocoin miner. , it's resource hungry. that's because took aproach. dump process output in file , read process. i'm not 100% happy have nodejs managing cryptocoin process, because don't know if node can handle it.

if there better way this, i'm happy ear it. thanks!

i did small snippet using npm package rotating-file-stream , seems work:

// stream.js const rfs = require('rotating-file-stream');  const stream = rfs('ping.txt', {   size: '500b', // rotate every 500 bytes written });  const stdin = process.openstdin();  stdin.pipe(stream); 

run command in terminal ping google.com | node stream.js


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -