Home > Package & System Management > top

Guide to the top Command: Real-time System Process Monitoring

top command (table of processes) is a tool used to monitor system process activity in real-time on Linux and Unix-like operating systems. It provides a dynamic overview of the current state of the system, including CPU usage, memory usage, and a list of running processes, making it essential for diagnosing performance issues and understanding resource usage. Through this guide, learn the basic usage and interaction features of the top command.

Overview of top

`top` periodically updates and displays the usage of system resources (CPU, memory, swap) and detailed information about each process. This allows you to easily identify which processes are using a lot of system resources and monitor the overall health of the system.

Main Functions of top

top command is primarily used for the following purposes:

Key Use Cases

  • {'key': 'System Monitoring', 'value': 'Check CPU, memory, and swap space usage in real-time.'}
  • {'key': 'Process Management', 'value': 'Understand the status of running processes and terminate or adjust priority if necessary.'}
  • {'key': 'Performance Issue Diagnosis', 'value': 'Analyze the causes of system slowdowns or unresponsiveness.'}
  • {'key': 'Resource Usage Optimization', 'value': 'Identify processes that consume excessive resources and take action.'}

Structure of top Output

top command displays information divided into two main parts:

Top Summary Information

Summarizes the overall state of the system.

  • uptime: Time elapsed since the system was booted.
  • load average: System average load over the last 1, 5, and 15 minutes (number of processes in the execution queue).
  • Tasks: Total number of processes, running, sleeping, stopped, and zombie processes.
  • %Cpu(s): CPU usage (us: user, sy: system, ni: nice, id: idle, wa: I/O wait, etc.).
  • MiB Mem: Total physical memory (RAM), usage, free space, and buffer/cache usage.
  • MiB Swap: Total swap memory, usage, and free space.

Bottom Process List

Displays detailed information about each running process. This list is sorted by CPU usage (%) by default.

  • PID: Process ID.
  • USER: User who executed the process.
  • PR: Priority.
  • NI: Nice value (used for priority adjustment).
  • VIRT: Total virtual memory used by the process.
  • RES: Amount of physical memory (RAM) used by the process (Resident Memory).
  • SHR: Amount of memory shared by the process (Shared Memory).
  • S: Process state (R: running, S: sleeping, Z: zombie, T: stopped, etc.).
  • %CPU: CPU usage of the process (real-time).
  • %MEM: Memory usage of the process (real-time).
  • TIME+: Total CPU time used by the process.
  • COMMAND: Command that started the process.

top vs htop

htop is an improved version of top, providing a more user-friendly interface (colors, mouse support) and features (scrolling, searching, direct termination). In most cases, htop is more convenient. If htop is not installed, you can install it through your package manager (e.g., sudo apt install htop).

Options and Interaction of the top Command

`top` allows you to specify command-line options at startup, and while running, you can use various key inputs to perform tasks such as changing the display style, sorting processes, and exiting.

1. Command-Line Startup Options

2. Interactive Keys While Running top

Generated command:

Try combining the commands.

Description:

`top` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn how to monitor system resource usage in real-time and diagnose issues through various examples of the `top` command.

Start Basic System Monitoring

top

Executes the most basic `top` command, updating and displaying the system's current state and process list every 3 seconds.

Check Top 5 Processes by CPU Usage (1-second interval)

top -d 1 -n 5

Runs `top` and updates only 5 times at a 1-second interval before exiting. This is useful for quickly identifying the cause of CPU load at a specific moment.

Monitor Only Processes of Specific User (root)

top -u root

Filters and displays only the processes executed by all `root` users on the system. Useful for tracking resource usage of system daemons or services.

Monitor Only Specific Process ID (PID)

top -p 12345

Monitors information about the single process with PID `12345` in real-time. Useful for accurately tracking resource consumption of a specific application.

Sort by Memory Usage (Interactive While Running)

top # then press Shift + m

While `top` is running, press `Shift + m` to sort the process list in order of memory usage. (Cannot be specified directly from the command line).

Terminate Specific Process (Interactive While Running)

top # then press 'k', enter PID, then optionally '9' for SIGKILL

While `top` is running, pressing the `k` key prompts you to enter the PID of the process to terminate. Enter the PID and press `Enter` to send the default SIGTERM signal. To force termination, enter signal number 9.



Same category commands