top

process managementLinux/Unix
The top command is one of the most frequently used commands in Linux/Unix-like operating systems. top The top command provides a dynamic real-time view of a running system. It displays system summary information and a list of processes or threads currently being managed by the Linux kernel. It shows CPU usage, memory usage, swap usage, and the most CPU-intensive tasks running on the system, all updated in real-time.

Quick Reference

Command Name:

top

Category:

process management

Platform:

Linux/Unix

Basic Usage:

top [options] [arguments]

Common Use Cases

  • 1

    Process monitoring

    Display dynamic information about running processes

  • 2

    Resource usage

    Monitor CPU, memory, and I/O usage of processes

  • 3

    Troubleshooting

    Diagnose issues with running processes

  • 4

    Scripting

    Use in shell scripts to monitor and manage processes

Syntax

top [options]

Options

Option Description
-b Batch mode, useful for sending output to other programs or to a file
-c Command line/program name toggle (shows full command line)
-d seconds Specify delay between screen updates
-H Show individual threads instead of processes
-i Ignore idle and zombie processes
-n iterations Specify number of updates before exiting
-o field Sort on specified field (e.g., %CPU, %MEM)
-p pid[,pid...] Monitor only specific process IDs
-s Secure mode, disables interactive commands
-u username Show only processes owned by specified user
-w Wide output mode, useful for full command lines

Examples

How to Use These Examples

The examples below show common ways to use the top command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Usage:

top

Launch top with default settings showing real-time process information.

top -u username

Show only processes owned by a specific user.

top -p 1234,5678

Monitor only specific processes by their PIDs.

Runtime Sorting:

top
Then press:
M (to sort by memory usage)
P (to sort by CPU usage)
T (to sort by time/cumulative time)

Interactive sorting during runtime by pressing various keys.

Display Options:

top -d 5

Set update interval to 5 seconds instead of the default.

top -b -n 3

Run in batch mode, displaying 3 iterations before exiting (useful for scripts).

top -c

Show the full command line for each process instead of just the command name.

Interactive Commands:

top
Then press:
h (for help)
k (to kill a process)
r (to renice a process)
f (to select fields to display)
z (to toggle color/mono)

Interactive commands that can be used while top is running.

Advanced Usage:

top -o %MEM

Sort display by memory usage (can use different fields like %CPU, TIME, etc.).

top -H

Show individual threads instead of summarizing all threads for each process.

top -b -n 1 | head -n 20 > system_status.txt

Capture a snapshot of top output to a file for system monitoring or troubleshooting.

Custom Configuration:

top
Customize the display, then press:
W (to save configuration)

Save your custom configuration for future top sessions.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Interactive Commands:

  • h or ?: Display help for interactive commands
  • k: Kill a process (provides a prompt for PID and signal)
  • r: Renice a process (change its priority)
  • q: Quit top
  • f: Add or remove fields from the display
  • o: Change the order of displayed fields
  • F: Select a field to sort on
  • W: Write current setup to ~/.toprc configuration file
  • space: Refresh the display immediately

Display Sorting Keys:

  • P: Sort by CPU usage (default)
  • M: Sort by memory usage
  • T: Sort by time (cumulative CPU time)
  • N: Sort by process ID
  • R: Sort by running time

Display Toggle Keys:

  • c: Toggle command name/full command line
  • i: Toggle idle processes display
  • t: Toggle task/CPU states display
  • m: Toggle memory information display
  • 1: Toggle single/separate CPU states
  • b: Toggle bold for highlight
  • x: Toggle highlight sort column
  • z: Toggle color/monochrome

Understanding the Header:

  • First line: System time, uptime, users, load averages (1, 5, 15 min)
  • Second line: Tasks summary (total, running, sleeping, stopped, zombie)
  • Third line: CPU states (user, system, nice, idle, etc.)
  • Fourth/Fifth lines: Memory usage (total, used, free, buffers/cache)

Process States:

  • R: Running or runnable (on run queue)
  • S: Sleeping in an interruptible wait
  • D: Uninterruptible sleep (usually I/O)
  • T: Stopped or traced
  • Z: Zombie process (terminated but not reaped by parent)

Common Fields in Process Display:

  • PID: Process ID
  • USER: User name
  • PR: Priority
  • NI: Nice value (negative=higher priority)
  • VIRT: Virtual memory used
  • RES: Resident memory used (RAM)
  • SHR: Shared memory size
  • S: Process status
  • %CPU: CPU usage
  • %MEM: Memory usage (RES)
  • TIME+: CPU time (hundredths of seconds)
  • COMMAND: Command name/line

Performance Tips:

  • Use top with -d to adjust refresh rate for less system impact
  • In batch mode (-b), limit the number of iterations (-n) for scripts
  • For low-resource systems, consider alternatives like htop, atop, or bpytop
  • Save frequently used configurations with 'W' to reduce setup time

Troubleshooting with top:

  • High CPU usage: Look for processes with high %CPU values
  • Memory issues: Sort by %MEM (press M) to find memory-intensive processes
  • System slowdown: Check load averages in the header (values consistently > # of cores indicates overload)
  • Process states: Large numbers of processes in D state may indicate I/O problems
  • Zombie processes: Check the zombie count in the tasks summary

Related Commands:

  • htop - Enhanced interactive process viewer with more features and visual display
  • ps - Provides a snapshot of current processes
  • vmstat - Reports virtual memory statistics
  • free - Displays amount of free and used memory
  • uptime - Shows how long the system has been running and load averages
  • kill - Send signals to processes (commonly used to terminate them)
  • nice - Run a program with modified scheduling priority
  • renice - Alter priority of running processes

Tips & Tricks

1

Use the -b option to batch mode

2

Use the -n option to specify the number of iterations

3

Use the -d option to delay between iterations

4

Use the -p option to specify a process ID to monitor

5

Use the -u option to display information about users

Common Use Cases

Process monitoring

Display dynamic information about running processes

Resource usage

Monitor CPU, memory, and I/O usage of processes

Troubleshooting

Diagnose issues with running processes

Scripting

Use in shell scripts to monitor and manage processes

System administration

Manage and optimize system resources

Related Commands

These commands are frequently used alongside top or serve similar purposes:

Use Cases

1

Process monitoring

Display dynamic information about running processes

2

Resource usage

Monitor CPU, memory, and I/O usage of processes

3

Troubleshooting

Diagnose issues with running processes

4

Scripting

Use in shell scripts to monitor and manage processes

5

System administration

Manage and optimize system resources

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the top command works in different scenarios.

$ top
View All Commands