The top command in Linux is an essential tool for system administrators and power users. It provides real-time insights into system performance and running processes, making it a vital utility for monitoring and troubleshooting. In this blog, we’ll break down the top command, explain its columns, provide practical examples, and demonstrate how to stress-test your system for learning and testing purposes.
For a step-by-step visual guide, watch this video.
What Is the Top Command in Linux?
The top command in Linux is a real-time system monitoring tool that displays information about system resource usage, including CPU, memory, and running processes. It’s an interactive and highly configurable utility that helps identify system bottlenecks and troubleshoot performance issues.
How to Run the Top Command
To launch the top command, simply open a terminal and type:
You’ll see system summary information at the top and a detailed process table below.
Understanding Columns in the Top Command
Here’s an overview of the key columns displayed in the process table:
- PID: Process ID of each running process.
- USER: The user who owns the process.
- PR: Priority of the process. Lower values mean higher priority.
- NI: Nice value, which affects process priority.
- VIRT: Total virtual memory used by the process, including memory swapped to disk.
- RES: Physical memory used by the process.
- SHR: Shared memory size.
- S: Process state (e.g., R for running, S for sleeping, Z for zombie).
- %CPU: CPU usage percentage.
- %MEM: Memory usage percentage.
- TIME+: Total CPU time the process has used since it started.
- COMMAND: The command or process name.
Simulating System Load Using the Stress Command
To fully understand how the top command in Linux works under load, you can use the stress
tool. This utility simulates CPU and memory usage, allowing you to test system performance.
Installing the Stress Command
For Rocky Linux or CentOS, use the following command:
Simulating CPU Load
Run the following command to generate load on 4 CPU cores for 30 seconds:
Open another terminal and run top
to observe CPU usage spikes in real time.
Simulating Memory Load
To simulate memory usage, run:
This creates two memory workers, each consuming 500 MB for 30 seconds. Use top
to monitor memory consumption.
Commonly Used Options and Examples
Sorting Processes
While running the top command in Linux, you can sort processes dynamically:
- Press
Shift + P
to sort by CPU usage (default). - Press
Shift + M
to sort by memory usage. - Press
Shift + T
to sort by process runtime.
Alternatively, use the -o
option to sort directly:
Filtering Processes
To filter processes, press o
and enter a query, such as:
COMMAND=ssh
USER=root
For a specific process, use:
Customizing Columns
Press f
to open the column selection menu. Use the arrow keys to navigate, the spacebar to toggle visibility, and Enter
to apply changes.
Highlight Active Processes
Press z
to visually highlight the most active process.
Change Update Interval
The default update interval is 3 seconds. Change it using the -d
option:
Saving Configurations
Press w
to save your custom settings for the next session.
Additional Features of the Top Command
- Tree View: Press
V
to toggle a hierarchical view of processes. - Batch Mode: Save output to a file for analysis:
- Killing Processes: Press
k
and enter the PID to terminate a process.
FAQs About the Top Command in Linux
1. What is the top command used for?
The top command monitors real-time system performance and running processes.
2. How do I sort processes in the top command?
Use Shift + P
, Shift + M
, or Shift + T
to sort by CPU, memory, or runtime.
3. Can I filter processes in the top command?
Yes, press o
to filter by criteria like command name or user.
4. How do I save the top command output to a file?
Run top -b -n 1 > file_name
to save a snapshot of the process table.
5. How do I terminate a process in the top command?
Press k
, enter the PID, and confirm to kill a process.
Conclusion
The top command in Linux is a powerful tool for monitoring system performance and troubleshooting. By understanding its columns, options, and features, you can effectively manage and optimize your Linux system. For a more hands-on approach, watch this video to see the top command in action.