Linux top command
The top
command in Linux is a real-time system monitoring tool that provides a dynamic view of system processes and resource usage, including CPU, memory, and load information. It allows administrators to identify processes consuming excessive resources and to manage system performance by prioritizing, killing, or renicing processes.
1. Basic Syntax
To run top
, simply enter:
The output is updated in real time, typically every few seconds, and provides a snapshot of the current system state.
2. Understanding the top
Command Output
The top
output is divided into several sections:
A. System Information Summary (Header Section)
The first few lines in top
provide a system overview, including uptime, load averages, tasks, CPU usage, and memory usage.
Uptime and Load Average
- 15:43:02: Current time.
- up 1 day, 2:10: System uptime (1 day, 2 hours, 10 minutes).
- 1 user: Number of users currently logged in.
- load average: CPU load averages over the last 1, 5, and 15 minutes.
Load average values provide a sense of CPU demand:
- Values close to or above the number of CPU cores indicate a heavily loaded CPU.
Tasks
- total: Total number of processes.
- running: Number of actively running processes.
- sleeping: Processes in a waiting state.
- stopped: Processes stopped or suspended.
- zombie: Zombie (defunct) processes that are waiting to be cleaned up.
CPU Usage
- us (user): CPU time spent on user-level processes.
- sy (system): CPU time on kernel-level processes.
- ni (nice): CPU time for processes with adjusted nice values.
- id (idle): Percentage of time the CPU is idle.
- wa (iowait): CPU time waiting for I/O operations.
- hi (hardware interrupts) and si (software interrupts): CPU time handling interrupts.
- st (steal): Time stolen by the hypervisor in virtualized environments.
Memory Usage
- total: Total physical memory.
- free: Available (unused) memory.
- used: Memory currently in use.
- buff/cache: Memory used for disk caching and buffers.
- Swap: Shows total, free, and used swap space, along with available memory for applications.
B. Process List (Main Section)
The lower part of top
lists individual processes with columns for process ID, user, priority, and resource usage.
Key columns include:
Column | Description |
---|---|
PID | Process ID. |
USER | User who owns the process. |
PR | Priority of the process. Lower values indicate higher priority. |
NI | Nice value, affecting process priority. |
VIRT | Virtual memory size of the process. |
RES | Resident memory used (actual physical memory). |
SHR | Shared memory used by the process. |
%CPU | CPU usage as a percentage. |
%MEM | Memory usage as a percentage of total RAM. |
TIME+ | Total CPU time consumed by the process. |
COMMAND | Command that started the process. |
Example:
In this example:
- Process
firefox
(PID 3245) is using 3.5% of the CPU and 1.2% of the memory. apache2
(PID 2134) is using 7% of the CPU and 0.6% of the memory.
C. Interacting with top
While top
is running, you can use keyboard shortcuts to interact with it:
q
: Quittop
.k
: Kill a process by specifying its PID.r
: Renice a process (change its priority).M
: Sort processes by memory usage.P
: Sort processes by CPU usage (default).T
: Sort processes by cumulative time.1
: Show CPU usage per core (useful on multi-core systems).h
: Display help and list all key commands.
3. Customizing top
Output
- Changing Update Interval: Press
d
and enter a new update interval in seconds. - Filtering by User: Press
u
and type a username to display only that user’s processes. - Highlighting Active Processes: Press
z
to toggle highlighting of active tasks.
4. Example Usage of top
- Monitoring CPU Usage: If a process is consuming too much CPU, you can identify it by sorting with
%CPU
. - Killing a Process: Use
k
, enter the PID of the process, and confirm to kill a misbehaving process. - Changing Priority (Renice): Use
r
to renice a process by adjusting itsNI
value, lowering its CPU priority.
5. Exiting top
To exit top
, simply press q
.
Summary
The top
command is a powerful tool for monitoring Linux system performance, especially useful for identifying and managing resource-intensive processes.