← Full reference

top

Processes and load in real time — keys, batch mode -b -n, columns, load average

top shows a system summary (uptime, load average, CPU, memory) and a process list that refreshes every 3 seconds. By default processes are sorted by CPU usage — the first thing to look at when "the server is slow". In batch mode (-b) top prints snapshots to stdout, so you can save its output or parse it in a script.

Syntax

top [options]
top -b -n 1            # one snapshot, non-interactive

Command-line options

OptionWhat it does
-bbatch mode: no keyboard control, output to stdout
-n numberhow many refreshes to do before exiting
-d secondsrefresh interval, may be fractional: -d 0.5
-p PID[,PID…]only these processes (up to 20)
-u useronly the user's processes (by effective UID); -U — by any of the UIDs
-o fieldsort by a field: %CPU, %MEM, RES, TIME+, PID; -o -PID / +PID — ascending / descending
-cshow the full command line instead of the name
-Hshow threads rather than processes
-1a separate line for each CPU core
-E k|m|gmemory units in the summary; -e k|m|g — in the process list
-w numberoutput width in batch mode (otherwise lines are truncated)
-ihide processes that are doing nothing
-ssecure mode: no k and r
-Olist all field names for -o

Keys in interactive mode

KeyWhat it does
qquit
h or ?help
P / M / T / Nsort by CPU / memory / CPU time / PID
< / >move the sort column left / right
Rreverse the sort order
ksend a signal to a process: asks for the PID and the signal (default 15 SIGTERM)
rchange a process's nice value (renice)
ushow only a user's processes (empty — everyone's)
o / Ofilter: COMMAND=nginx, %CPU>5.0; = — reset filters
L / &find a string / next match
cfull command line on/off
1per-core usage on/off
Hthreads on/off
Vprocess tree (parent → children)
ihide idle processes
d or schange the refresh interval
E / echange memory units in the summary / in the list
fchoose and order columns
x / yhighlight the sort column / running processes
l / t / mhide/show the load average / CPU / memory line
Wsave the current settings to ~/.config/procps/toprc
spacerefresh now

Reading the summary

top - 15:29:20 up 19:38,  1 user,  load average: 5.28, 6.35, 5.84
Tasks: 104 total,   1 running, 103 sleeping,   0 stopped,   0 zombie
%Cpu(s): 12.0 us,  9.6 sy,  0.0 ni, 73.5 id,  1.2 wa,  0.0 hi,  3.6 si,  0.0 st
MiB Mem :   3918.8 total,    640.2 free,   2259.3 used,   1237.6 buff/cache
MiB Swap:   4096.0 total,   2968.7 free,   1127.3 used.   1659.5 avail Mem
FieldMeaning
up 19:38how long the system has been running
load averageaverage CPU queue length over 1, 5 and 15 minutes: processes running or waiting for the CPU, plus those waiting for disk (state D)
us / syCPU time spent on user code / on the kernel
nion processes with raised nice (low priority)
ididle
wawaiting for I/O — a high value = the disk is the bottleneck
hi / sihandling hardware / software interrupts
sttime "stolen" from the virtual machine by the hypervisor
free / used / buff/cachefree / used / file cache (freed on demand)
avail Memhow much memory is really available to new programs — look at this, not at free

Load average is compared with the number of cores (nproc): on 4 cores 4.00 is full utilization, 8.00 — a queue twice as long as the number of cores. A rising first number — load is growing, a falling one — load is easing.

Process columns

ColumnMeaning
PID / USERprocess number / effective user
PRscheduler priority (20 + NI for normal processes; rt — real-time)
NInice: from -20 (high priority) to 19 (low)
VIRTall virtual memory, including what isn't loaded (KiB)
RESphysical memory actually used (KiB) — the key number
SHRthe part of RES shared with other processes (libraries)
Sstate: R running, S sleeping, D waiting for disk (uninterruptible), T stopped, t under a debugger, Z zombie, I idle kernel thread
%CPUCPU usage over the last interval; on several cores it can exceed 100
%MEMRES as a percentage of total memory
TIME+total CPU time, with hundredths
COMMANDprogram name (c — full command)

Examples

# a snapshot to a file: the first 20 lines
top -b -n 1 | head -20 > snapshot.txt

# top 5 by memory
top -b -n 1 -o %MEM | sed -n '8,12p'

# watch specific processes
sleep 300 &
top -p "$!"
top -b -n 3 -d 1 -p "$(pgrep -d, sleep)"

# one user's processes with full commands, wider than the screen
top -b -n 1 -u www-data -c -w 200

# record the load every 5 seconds for a minute
top -b -d 5 -n 12 | grep '^top -' >> load.log

# only the load average — without top
cat /proc/loadavg
uptime

Pitfalls

  • The first top -b -n 1 snapshot is inaccurate for %CPU: the percentages have nothing to compare against. For honest numbers, take two refreshes and use the last one: top -b -n 2 -d 1.
  • A small free is normal. Linux uses free memory for the file cache. A memory shortage shows up as a small avail Mem and a growing used in swap.
  • VIRT isn't memory consumption. It's the whole address space, including reserved and untouched parts. Look at RES.
  • Load average isn't CPU percent. Processes in state D (waiting for disk or NFS) raise the load even if the CPU is idle. High load with a large id and a large wa — a disk problem.
  • In a container top shows the load average, CPU and memory of the whole host, but only its own processes. The container's limits aren't visible there.
  • Without -w, lines in batch mode are truncated to the terminal width (or 80 characters), and long commands are lost.
  • -p takes a comma-separated list, not space-separated: pgrep -d, name gives the right format.

See also

ps · kill · nice · pgrep · proc · system-info