40

The widget iStat Pro allows to monitor different system stats like

  • CPU usage
  • network bandwidth in/out
  • memory usage
  • ...

How can I view such system stats in the Terminal?

enter image description here

gentmatt
  • 49,722

7 Answers7

67

Depending on what you want displayed there are several options available using Terminal:

  • top: real-time sorted display of running processes such as memory and CPU usage
  • iostat: I/O summary statistics for terminal, device and CPU operations
  • vm_stat: Mach virtual memory statistics
  • diskutil list: disk/volume capacity
  • df: used/free space on mounted partitions
  • fs_usage: real-time file activity for both disk and network
  • nettop: updated information about the network (a bit like top for net I/O)
  • w: who is logged in and what they are doing; plus a brief system load summary
  • ifconfig and ipconfig: network interface and IP protocol details

Most of these commands have a huge list of options.  It's probably best to consult the man pages for details.

I usually rely on:

  • top -u -s 10 to identify CPU-hogging processes;
  • fs_usage -f filesys or fs_usage -f network to identify processes generating a lot of disk/network load.
rwarvi
  • 9
nohillside
  • 100,768
9

The iStats ruby gem lets you see the CPU temperature via the command-line.

Installation

$ gem install iStats

Usage

$ istats

Screenshot

Chris911
  • 2,867
6

You can use top. It'll show CPU & RAM usage together with all the processes. It'll also show you network packets in/out, and discs data read/written.

Kyle Cronin
  • 23,242
  • 21
  • 83
  • 143
Uko
  • 957
4

The above are great commands. Also, I like to use

$ du -ks *

To show how much disk space all the folders below my current directory are using.

This command lists the top 10 directories in order of size:

$ du -sh * | sort -nr | head -10

For an overall system stats tool, I like the python glances better than top:

https://nicolargo.github.io/glances/

This is how you can install it on MacOS using the Homebrew package manager:

$ brew install glances

This is how you can install it on Debian/Ubuntu:

$ sudo apt-get install glances

This is how you can install it on CentOS/Red Hat:

# yum  install glances
2

My preferred way to see CPU usage with a single command that returns instantly is

ps -axro pcpu | awk '{sum+=$1} END {print sum}'

-ax: all processes, including non-terminal processes and ones you don't own.

-r: sort by cpu usage

-o pcpu: only output the %cpu field. %cpu is a 1-minute average of the process, but will only average over the lifetime of the process if its lifetime is <1min.

awk: sum the first column in each line and print the sum at the end.

This is a per-core CPU metric, so on a 12 core CPU you can get up to 1200; you're not capped at 100.

2

If you are having network problems, especially with a server exposed to the Internet, lsof(1) can be extremely useful. It lists all open files.

For example, lsof | fgrep '/Library/WebServer/ will show you all the files that are open on your web server. I've been using this a lot to find and ban "leeches" and "bots" who are reducing my bandwidth by automatically sucking down file after file.

daviesgeek
  • 38,901
  • 52
  • 159
  • 203
1

Here are two commands for determining your local and public IP:

Local IP

ipconfig getifaddr en1

This is when using Wi-Fi. For ethernet use en0.

Public IP

curl whatismyip.org
gentmatt
  • 49,722
  • 1
    On current MacBook Airs (which don't have an ethernet port) the Wi-Fi device code is en0. You can find out what it is with networksetup -listallhardwareports | grep -E '(Wi-Fi|AirPort)' -A 1 | grep -o "en.". – Lri Feb 23 '12 at 23:05