LinuxCommandLibrary

top

Display and manage running processes

TLDR

Start top

$ top
copy

Do not show any idle or zombie processes
$ top [[-i|--idle-toggle]]
copy

Show only processes owned by given user
$ top [[-u|--filter-only-euser]] [username]
copy

Sort processes by a field
$ top [[-o|--sort-override]] [field_name]
copy

Show the individual threads of a given process
$ top [[-Hp|--threads-show --pid]] [process_id]
copy

Show only the processes with the given PID(s), passed as a comma-separated list. (Normally you wouldn't know PIDs off hand. This example picks the PIDs from the process name)
$ top [[-p|--pid]] $(pgrep [[-d|--delimiter]] ',' [process_name])
copy

Display help about interactive commands
$ <?>
copy

SYNOPSIS

top [options]

PARAMETERS

-b
    Starts top in batch mode, sending output to stdout without interactive commands. Useful for scripting.

-d
    Specifies the delay time (in seconds) between screen updates. Default is 3 seconds.

-n
    Specifies the maximum number of iterations (updates) top should produce before exiting.

-p
    Monitors only the specific process IDs (PIDs) provided. Multiple PIDs can be separated by commas.

-u
    Monitors processes owned by a specific user or effective UID. Can be a username or UID.

-H
    Shows individual threads rather than grouping them under their parent process.

-i
    Does not show idle or zombie processes in the process list, focusing on active processes.

-s
    Starts top in secure mode, disabling potentially dangerous interactive commands like 'k' (kill) and 'r' (renice).

-c
    Toggles the display of the full command line for processes instead of just the command name.

-S
    Starts top in cumulative mode, where each process reports its CPU time to include that of its dead children.

DESCRIPTION

top provides a dynamic, real-time view of a running Linux system.
It displays system summary information at the top, including uptime, load averages, tasks, CPU states, and memory/swap usage.
Below the summary, it lists processes or threads currently being managed by the kernel, ordered by CPU usage by default. Users can interact with top to sort processes by different criteria (e.g., memory, PID), kill processes, renice them, or change the refresh interval.
It's an indispensable tool for system monitoring, performance analysis, and troubleshooting, offering a snapshot of resource consumption and process behavior.

CAVEATS

While powerful, top itself can consume CPU, especially with short refresh intervals or when monitoring a very large number of processes.
Its default display and interactive commands can be overwhelming for novice users.
It's designed for real-time snapshots, not for long-term historical data logging, for which tools like sar or specific monitoring daemons are better suited.

KEY INTERACTIVE COMMANDS

Once top is running, users can press various keys for interactive actions:
k: Kill a process (prompts for PID).
r: Renice a process (change its priority).
d or s: Change the delay time between updates.
u: Filter processes by user.
P: Sort processes by CPU usage (default).
M: Sort processes by resident memory usage (%MEM).
T: Sort processes by time or cumulative time.
1: Toggle display of individual CPU cores.
q: Quit top.

UNDERSTANDING OUTPUT COLUMNS

The process list in top displays various columns:
PID: Process ID.
USER: Owning user name.
PR: Priority of the task.
NI: Nice value of the task.
VIRT: Virtual memory used by the process.
RES: Resident (physical) memory used.
SHR: Shared memory used.
S: Process status (e.g., R=running, S=sleeping, Z=zombie).
%CPU: CPU usage since last update.
%MEM: Physical memory usage.
TIME+: Total CPU time used by the task.
COMMAND: Command name or full command line.

HISTORY

top has been a fundamental system monitoring utility in Unix-like operating systems for decades, with its origins tracing back to BSD systems. The version commonly found on Linux distributions is part of the procps-ng suite of utilities, which continues to be actively maintained and enhanced. It has evolved significantly over time, adding features like multi-CPU core monitoring and improved interactive capabilities, making it a cornerstone for system administrators and developers.

SEE ALSO

htop(1), ps(1), uptime(1), free(1), vmstat(8), sar(1)

Copied to clipboard