top Command in Linux: Syntax, Options & Process Monitoring Examples

top shows a live summary of CPU, memory, and tasks, plus a sortable process list. Batch mode (-b) prints snapshots for scripts and logs without the interactive TUI.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

top Command in Linux: Syntax, Options & Process Monitoring Examples
Tested on Ubuntu 25.04 (Plucky Puffin)
Package top (procps 4.0.4)
procps-ng
Applies to Ubuntu, Debian, RHEL, Fedora, SUSE
Privilege sudo or root
Man page top(1)
Scope top shows a live summary of CPU, memory, and tasks, plus a sortable process list. Batch mode (-b) prints snapshots for scripts and logs without the interactive TUI.
Related guides nice
Linux commands

top — quick reference

Batch and iteration (scripts and logs)

When to use Command
One snapshot to stdout (non-interactive) top -b -n 1
Multiple snapshots then exit top -b -n 5
Change delay between batch iterations (seconds) top -b -d 2 -n 3
Exit after N screen updates (interactive) top -n 10

Sorting and display

When to use Command
Sort by memory percent top -b -n 1 -o %MEM
Sort by CPU percent top -b -n 1 -o %CPU
Sort by process ID top -b -n 1 -o PID
List available sort fields and exit top -O
Show threads for each task top -b -n 1 -H
Set screen width (columns) top -b -n 1 -w 120

Filter by process or user

When to use Command
Monitor only listed PIDs top -b -n 1 -p 1234,5678
Show only processes owned by user top -b -n 1 -u username
Same as -u (filter-only euser) top -b -n 1 -U username

Memory scale and toggles

When to use Command
Summary memory scale k/m/g/t/p/e top -b -n 1 -E g
Per-task memory scale k/m/g/t/p top -b -n 1 -e m
Toggle cumulative time mode (flip last state) top -S
Toggle idle tasks (flip last state) top -i
Toggle command line vs name (flip last state) top -c
Toggle single-CPU view (flip last state) top -1

Security and help

When to use Command
Secure mode (fewer interactive commands) top -s
Show help and exit top -h
Show version and exit top -V

top — command syntax

Synopsis from top --help on Ubuntu 25.04 (procps 4.0.4):

text
top [options]

Interactive top reads from /proc and refreshes the display. For scripts, use batch mode (-b) with -n so the command exits instead of filling the terminal.


top — command examples

Essential One-shot process list for scripts

Use batch mode when you need a process table in a log or CI artifact — not an interactive session.

Run the command:

bash
top -b -n 1 | head -20

Sample output (first lines vary by host load):

text
top - 14:20:21 up  5:26,  8 users,  load average: 1.74, 0.81, 0.81
Tasks: 242 total,   2 running, 240 sleeping,   0 stopped,   0 zombie
%Cpu(s): 19.0 us, 47.6 sy,  0.0 ni,  4.8 id,  0.0 wa,  0.0 hi, 28.6 si,  0.0 st
MiB Mem :   5292.8 total,    393.8 free,   2424.9 used,   2751.6 buff/cache
MiB Swap:   2477.0 total,   1293.9 free,   1183.1 used.   2867.9 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND

Pipe to head when you only need the header plus top consumers.

Essential Find top memory consumers

When a host feels slow, sort by %MEM to see which processes hold RAM.

Run the command:

bash
top -b -n 1 -o %MEM | head -12

Sample output:

output
PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  21458 root      20   0 3459544   1.1g  50096 R  91.7  21.5  41:24.41 hugo

Cross-check with ps if you need custom columns.

Common Monitor specific PIDs only

Narrow the view when you already know which process misbehaves (comma-separated list).

Run the command:

bash
top -b -n 1 -p $$

Sample output:

output
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  37261 root      20   0   13364   7056   3928 S   0.0   0.1   0:00.07 bash

Replace $$ with the target PID from pgrep or ps.

Common Show only one user's processes

Useful on shared servers to see what a service account is running.

Run the command:

bash
top -b -n 1 -u root | head -12

Only processes whose effective user matches root appear in the task list.

Common List sortable field names

Before scripting -o, print the field names top accepts.

Run the command:

bash
top -O

Sample output includes lines such as PID, %CPU, %MEM, COMMAND, TIME+ — exact list matches your procps build.

Advanced Repeated snapshots with delay

Capture several samples spaced apart — lighter than leaving interactive top open.

Run the command:

bash
top -b -d 1 -n 3 | tail -8

-d 1 waits one second between iterations; -n 3 stops after three updates.


top — when to use / when not

Use top when Use something else when
  • You need a live view of CPU, memory, load, and processes
  • You want one-off snapshots in scripts (top -b -n 1)
  • You are sorting or filtering by PID, user, or %MEM/%CPU on the fly
  • top is already installed (procps default)
  • You want a friendlier TUI with mouse support → install htop
  • You need a static process list → ps
  • You need long-term metrics → sar or your monitoring stack
  • You need per-disk or per-interface detail → iotop, vmstat

top vs htop

top htop
Ships by default Yes (procps) Separate package
Interface Keyboard-driven TUI Mouse-friendly TUI
Scripting top -b -n 1 Not designed for batch
Extra features Field toggles, secure mode Tree view, scroll, color themes

Both read /proc. Use top when it is the only tool available; install htop for daily interactive triage.


top — interview corner

What is the top command used for?

top displays a real-time view of system summary (uptime, load, CPU, memory) and a process table sorted by CPU by default. It refreshes until you quit (q).

For scripts, top -b -n 1 prints one snapshot and exits.

A strong answer is:

"top is the classic procps monitor for live CPU, memory, and process data. I use interactive top on servers and top -b -n 1 in scripts for a one-line snapshot."

How do you run top non-interactively?

Use batch mode: -b sends output to stdout; -n N limits iterations so the command exits.

Example: top -b -n 1 | head -20

A strong answer is:

"top -b -n 1 gives a single batch snapshot suitable for logs — without batch mode top expects a TTY and keeps running."

How do you sort top by memory?

Pass -o %MEM (field names from top -O). Example: top -b -n 1 -o %MEM

A strong answer is:

"I use -o %MEM in batch mode to list the biggest RAM users — field names come from top -O on that procps version."

When would you use top instead of ps?

ps is a point-in-time list; top refreshes and shows system-wide CPU/memory headers. Use top when watching behavior over seconds; use ps in scripts when you need lightweight output.

A strong answer is:

"ps for a quick static list in scripts; top when I'm watching load spike or sorting live by CPU or memory."


Troubleshooting

Symptom Likely cause Fix
top hangs in scripts Missing -b on non-TTY Use top -b -n 1
Empty or partial output piped Buffering / huge process list Add | head or raise -n
Cannot see other users' processes Run as normal user sudo top for full view
invalid option on -o Wrong field name for this procps Run top -O for valid names
High %wa in header I/O wait Investigate disk with iostat command and iotop

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.