How can I get the top output of a command in real time in Mac Os

I want to control the server and display the top output of the command in real time on a web page. error: initializing curses occurs after starting the program on Mac OS. I don’t know how to deal with this, my question is how to get the top output of the command 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); }); 
+4
source share
2 answers

If you run top in the command window, it will update the data on the screen using the curses library. Try using the -b option in batch mode to see if this node will allow its output to be sent to your

+4
source

It looks like the OSX top command should be called with the -l flag (other UNIX use -b ). With this flag, the "curses" library is no longer required (this library makes the formatting fantasy for interactive mode the default), so the pipe starts working.

Here is the code to get you started:

 var top = spawn("top", ["-l 0"]); top.stdout.pipe(process.stdout); top.stderr.on("data", function(data) { console.log("ps stderr: " + data); }); 
+1
source

All Articles