Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Processes and Signals

Linux top command

Reference for the Linux top command: read the summary lines, sort by CPU or memory, use batch mode for scripts, and learn the interactive keys.

top is a live, refreshing view of the machine: a few summary lines (uptime, load, tasks, CPU and memory) above a table of processes sorted by CPU use. Unlike ps it keeps updating, usually every three seconds, so you see what is busy now.

It is interactive by default: you press keys to sort, kill processes or change what is displayed. For scripts and logs there is batch mode (-b), which prints plain text instead of drawing on the screen. Many people also use htop, a friendlier alternative that is not installed everywhere; top is present on practically every Linux system.

Terminal
top
top -b -n 1 | head -15

Try it

In the sandbox there is no terminal to draw on, so use batch mode with a fixed number of iterations.

Bash One batch snapshot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Watch one process

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
top [-b] [-n COUNT] [-d SECONDS] [-p PID,...] [-u USER] [-o FIELD] [-H]
Option Description
-b Batch mode: plain text, no keyboard input, suitable for pipes and files.
-n COUNT Quit after COUNT updates.
-d SECONDS Delay between updates (fractions allowed, for example 0.5).
-p PID,... Show only these process IDs.
-u USER Show only processes of this user.
-o FIELD Sort by FIELD (for example %MEM, TIME+); -FIELD for ascending.
-H Show individual threads instead of processes.
-c Show full command lines.
-i Hide idle processes.

Reading the display:

  • load average: 0.50, 0.40, 0.30 is the average number of runnable (and waiting) tasks over 1, 5 and 15 minutes. Compare it with the number of CPU cores.
  • %Cpu(s): us user, sy kernel, ni nice, id idle, wa waiting for I/O, hi/si interrupts, st stolen by the hypervisor.
  • MiB Mem: free, used, buff/cache. The kernel uses spare memory for caching, so a small free is normal. Look at avail Mem.
  • Per process: PID, USER, PR priority, NI nice value, VIRT, RES (resident) and SHR memory, S state, %CPU, %MEM, TIME+, COMMAND.

Interactive keys: q quit, h help, P sort by CPU, M by memory, T by time, k kill (asks for PID and signal), r renice, 1 per-CPU lines, u filter by user, c toggle command line, H threads, d change interval, W save settings.

Returns: 0 on normal exit; non-zero on bad options.

Usage notes

  • %CPU can exceed 100 on multi-core machines: 100 means one full core. Press I to divide by the number of cores (Irix/Solaris mode).
  • The first update is misleading for CPU: it reports the average since boot. Use -n 2 and read the second block in scripts.
  • Memory columns: VIRT is address space (often huge), RES is what is actually in RAM. Judge a process by RES, and the machine by avail Mem.
  • High wa means the CPU waits for disk; look at vmstat and iostat. High st on a VM means a noisy neighbour.
  • Saving a view: W writes ~/.config/procps/toprc, so your sort order and columns return next time.
  • Logs: top -b -n 1 -o %MEM | head -20 in a cron job gives a cheap record of the biggest memory users.