I want to monitor server and show top command output in real time on webpage.
there is an error error: initializing curses after program started on Mac OS.
I don't know how to deal with it, my question is how to get top command output in real time via Node.JS?
var spawn = require('child_process').spawn,
com = spawn('/usr/bin/top', []);
var io = require('socket.io').listen(5555);
com.stdout.on('data', function(data){
io.sockets.emit('get result', data);
}
);
com.stderr.on('data', function(data){
console.log('stderr: ' + data);
});
If you run top in a command window, it updates the data on the screen, using the curses library. Try using the batch-mode '-b' option on top to see if that'll allow node to pipe its output into your
Looks like OSX's top command has to be called with -l flag (other UNIXes use -b).
With that flag "curses" library is no longer required (this lib does fancy formatting for default interactive mode) so piping begins to work.
Here is the code to start with:
var top = spawn("top", ["-l 0"]);
top.stdout.pipe(process.stdout);
top.stderr.on("data", function(data) {
console.log("ps stderr: " + data);
});