GICT knowledge base
How to Use the top Command in Bash
Published
The top command is one of the most commonly used commands in Linux and Unix-based systems for monitoring system performance in real-time. It provides a dynamic, real-time view of what is happening on your system, including currently running processes, CPU usage, memory consumption, and more. This guide will explore the various aspects of the top command, including its features, how to use it effectively, and tips for interpreting its output.
Understanding the top Command
When you run the top command in a terminal, you access an interface that displays a constantly updating list of processes. The interface shows information such as:
- Process ID (PID)
- Memory usage
- CPU usage
- Running time
- Command that started the process
This data is invaluable for administrators and users looking to monitor system performance and troubleshoot issues related to resource usage.
Basic Usage
To run the top command, simply open your terminal and type:
top
After pressing Enter, you will see a display similar to the following:
top - 16:43:54 up 1:23, 2 users, load average: 0.10, 0.05, 0.01
Tasks: 124 total, 1 running, 123 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.0 us, 0.5 sy, 0.0 ni, 98.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 7755 total, 1630 free, 3322 used, 3803 buff/cache
MiB Swap: 2048 total, 2048 free, 0 used. 4780 avail Mem
Breaking Down the top Output
The output is divided into several sections, each providing different types of information:
Summary Information
The summary area at the top shows system information including:
- Current time
- System uptime (how long the system has been running)
- Number of users currently logged in
- Load average for the last 1, 5, and 15 minutes
Task Information
The next line provides an overview of tasks, displaying:
- Total tasks (the number of processes running)
- Running tasks (currently active processes)
- Sleeping tasks (idle processes)
- Stopped tasks and Zombie tasks (tasks that have completed but still have an entry in the process table)
CPU Usage
The %Cpu(s) line displays CPU usage information, showing how much CPU is being consumed by different types of processes:
- us: User space processes
- sy: System processes
- ni: Nice processes
- id: Idle processes
- wa: I/O wait
- hi: Hardware interrupts
- si: Software interrupts
- st: Steal time (when running a virtual machine)
Memory Information
The memory information line provides details about RAM and swap memory usage:
- Total memory available
- Free memory
- Used memory
- Cache and buffers usage
This detailed view helps you gauge how much of your system’s resources are currently being utilized.
Commonly Used Commands within top
While the top command itself provides valuable information, it also includes various keyboard shortcuts that enhance its functionality:
Sorting Processes
You can sort the displayed processes by different criteria:
- Press
m: Sort by memory usage. - Press
p: Sort by CPU usage. - Press
t: Sort by time (how long the process has been running).
Changing Refresh Rate
You can change how often top refreshes the screen:
- Press
d: Enter new refresh interval in seconds.
Killing a Process
You can also terminate a process directly from the top interface:
- Press
k: Enter the PID of the process you want to kill, followed by the signal to send (default isSIGTERM). Then pressEnter.
Searching for Processes
If you want to find a specific process, you can search for it:
- Press
/: Type the name of the process you want to search for and pressEnter.
Exiting top
To exit the top command, simply press:
q
Practical Examples of Using the top Command
Example 1: Monitoring System Resource Usage
You can use the top command to monitor CPU and memory usage in real-time. Simply run top in your terminal, and look for processes that are consuming a lot of resources. Use the sorting commands to quickly identify which processes are hogging resources.
Example 2: Identifying Unresponsive Applications
If you notice your system getting slow, running top can help you identify unresponsive applications consuming excessive CPU or memory. You could then kill those processes directly from the top interface.
Example 3: Analysis of Background Processes
Using top is also helpful for monitoring any background tasks or services. For instance, if you’re running a web server or database, you can check their resource consumption and ensure they remain within acceptable limits.
Advanced Features of top
Customizing the Display
The top command allows you to customize the display settings to show only the information that is relevant to your needs. You can set parameters for which fields are shown, their order, and how they are sorted. To customize the display:
Press f to enter the field management interface.
From here, you can toggle various fields to display or hide them as you prefer.
Using Batch Mode
If you want to save the output of the top command for later analysis or for logging, you can run it in batch mode:
top -b -n 1 > output.txt
This command runs top in batch mode, capturing one iteration of the display in a file called output.txt. You can specify other options to capture additional iterations or to alter the format as needed.
Using the htop Command
While top is great, you may also find htop to be a more user-friendly alternative. htop provides a visually appealing and easy-to-navigate interface that allows you to perform actions just like in top but with enhanced features.
To install htop, you can typically use your package manager:
sudo apt install htop
Once installed, simply run htop in your terminal to see your processes with a color-coded display and more manageable controls.
Conclusion
The top command is an invaluable tool for monitoring and managing processes in Linux. By learning how to leverage this command effectively, you can keep your system’s performance optimal, troubleshoot problems promptly, and make informed decisions on resource management.
Make it a habit to check top regularly, especially when your system exhibits sluggish behavior. Understanding how to interpret the output and apply its features will empower you to maintain better control over your processes.
As you become more comfortable with the top command, consider exploring other related commands, like htop, to gain deeper insights into your system’s resource usage. With practice, monitoring and managing your system resources will become second nature.
Related guides
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
