Reading application stdout data using node.js -
let's take e.g. "top" application displays system information , periodically updates it.
i want run using node.js , display information (and updates!). code i've come with:
#!/usr/bin/env node var spawn = require('child_process').spawn; var top = spawn('top', []); top.stdout.on('readable', function () { console.log("readable"); console.log('stdout: '+top.stdout.read()); });
it doesn't behave way expected. in fact produces nothing:
readable stdout: null readable stdout: readable stdout: null
and exits (that unexpected).
top application taken example. goal proxy updates through node , display them on screen (so same way running top directly command line).
my initial goal write script send file using scp. done , noticed missing progress information scp displays. looked around @ scp node modules , not proxy it. backtracked common application top.
top
interactive console program designed run against live pseudo-terminal.
as stdout
reads, top
seeing stdin
not tty , exiting error, no output on stdout
. can see happen in shell if echo | top
exit because stdin not tty.
even if running though, it's output data going contain control characters manipulating fixed-dimension console. (like "move cursor beginning of line 2"). interactive user interface , poor choice programmatic data source. "screen scraping" , interpreting data , extracting meaningful information going quite difficult , fragile. have considered cleaner approach such getting data need out of /proc/meminfo
file , other special files kernel exposes purpose? top
getting data readily-available special files , system calls, should able tap data sources convenient programmatic access instead of trying screen scrape top.
now of course, top
has analytics code averages , forth may have re-implement, both screen-scraping , going through clean data sources have pros , cons, , aspects easy , difficult. $0.02 focus on data sources instead of trying screen scrape console ui.
other options/resources consider:
- the
free
command suchfree -m
vmstat
- and other commands described in article
- the expect program designed automate console programs expect terminal
and clear, yes possible run top
child process, trick thinking there's tty , associated environment settings, , @ data writing. it's extremely complicated , analogous trying weather taking photo of weather channel on tv screen , running optical character recognition on it. points style, there easier ways. expect
command if need research more tricking console programs running subprocesses.
Comments
Post a Comment