top and htop: watch processes live
top shows a live, updating list of the processes using the most CPU, together with the system's load, memory and swap. It is the first tool to open when a server is slow.
top [-d seconds] [-u user] [-p pid]
Reading the header
top - 09:21:07 up 3 days, 14:02, 2 users, load average: 3.12, 2.40, 1.95
Tasks: 214 total, 2 running, 212 sleeping, 0 stopped, 0 zombie
%Cpu(s): 41.2 us, 6.3 sy, 0.0 ni, 47.9 id, 4.1 wa, 0.0 hi, 0.5 si, 0.0 st
MiB Mem : 15890.4 total, 812.6 free, 9103.2 used, 6382.7 buff/cache
MiB Swap: 2048.0 total, 2040.1 free, 7.9 used. 6787.2 avail Mem
- Load average: the average number of processes running or waiting, over 1, 5 and 15 minutes. Compare it with the number of CPU cores (
nproc): a load of 3 on a 4-core server is fine, on a 1-core server it is overloaded. On Linux, processes waiting for disk count too. - us / sy: CPU time in programs / in the kernel. id: idle.
- wa (iowait): CPUs idle while waiting for disk. High values point to slow or overloaded storage.
- st (steal): time a virtual machine wanted CPU but the host gave it to someone else.
- avail Mem: how much memory new programs could use. Linux uses free memory for disk cache (buff/cache), so a low free value is normal; a low avail value and growing swap use are not.
Keys inside top
| Key | Action |
|---|---|
| P / M / T | Sort by CPU / memory / running time |
| 1 | Show each CPU separately |
| c | Show full command lines |
| u | Only one user's processes |
| o | Filter, for example COMMAND=php |
| k | Kill a process (asks for PID and signal) |
| H | Show threads |
| W | Save the current view as your default |
| q | Quit |
Examples
top # interactive
top -u www-data # one user
top -p 950,951 # selected PIDs
top -b -n 1 | head -20 # one snapshot as text, for scripts or e-mails
top -b -n 1 -o %MEM | head -15 # snapshot sorted by memory (procps-ng)
htop and btop
htop is a friendlier top with colours, mouse support, a tree view (F5) and easy searching (F3) and killing (F9). btop adds graphs for CPU, memory, disks and network. Install them with your package manager.
Quick checks without top
uptime # load average
nproc # number of CPU cores
free -h # memory and swap
vmstat 2 5 # CPU, memory, swap and I/O every 2 seconds, 5 times
iostat -xz 2 # disk utilisation and wait times (sysstat package)
On AIX use topas or nmon, on Solaris prstat, on HP-UX glance or top. To end a runaway process, see kill.