4

I am writing a program to read cpu and memory statistics from a remote host. The remote host can be Linux or Mac. So I need a command which returns the cpu/memory usage as output. I have found the command for Linux which is vmstat. But I couldn't find a good one for Mac. I know there is one command vm_stat on mac but it only returns memory usage. Is there a command for CPU usage as well? I have searched that most people use top. But top is good at monitoring and it would be hard to parse the result if I analyze its output in my program. I want to use one shipped with MacOS not a third party library.

2

If you are looking to capture the total utilized CPU and Memory for all process, you could use this command that is native to Macs:

ps -A -o %cpu,%mem | awk '{ cpu += $1; mem += $2} END {print cpu , mem}'
  • Great answer! Also, if you want a some labels on the output, you can change it to {print "CPU: "cpu"% MEM: "mem"%"}. That will print something like CPU: 16.3% MEM: 75.1% – wisbucky Jul 17 at 21:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .