How to use the command 'top' (with examples)
- Linux
- December 17, 2024
The ’top’ command is an essential utility for system administrators and users who want to monitor system performance and track the behavior of running processes in a dynamic, real-time format. It provides a frequently updated view of processes in a system, which can be particularly useful for understanding resource consumption, finding performance bottlenecks, or ensuring system stability. Its interactive mode allows users to manipulate and sort the data being displayed according to specific needs, providing thorough insights into system operations.
Start with ’top'
Code:
top
Motivation: The basic invocation of the ’top’ command is often the first step towards system monitoring. This command provides an immediate, high-level view of system performance, making it a critical tool for identifying resource-intensive processes. Whether you’re troubleshooting a sluggish computer or conducting a routine check, starting with ’top’ gives you a snapshot of the current process landscape.
Explanation: Simply typing top
with no options at the command prompt starts the default interface. It displays a list of the currently running processes, updated every few seconds. This helps in visualizing system load and CPU utilization directly on your terminal screen.
Example output:
top - 12:17:03 up 6 days, 20:54, 1 user, load average: 0.08, 0.10, 0.09
Tasks: 123 total, 2 running, 121 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.2 us, 0.3 sy, 0.0 ni, 98.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8064324 total, 1021496 free, 2439708 used, 4603120 buff/cache
KiB Swap: 2097148 total, 2097136 free, 12 used. 5263444 avail Mem
Exclude Idle and Zombie Processes
Code:
top -i
Motivation: When performance issues arise, and you need to concentrate solely on processes actively consuming resources, excluding idle and zombie processes is beneficial. Idle processes do not currently use CPU resources, and zombie processes are essentially cleaned-up remnants waiting to be removed. Focusing on active processes simplifies analysis and aids in quick identification of resource hogs.
Explanation: The -i
flag, when added to the ’top’ command, ensures idle processes and zombie processes are not displayed in the output. This adjustment streamlines the process list, putting emphasis on processes that actively require attention.
Example output:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4140 user1 20 0 1961700 163776 28120 S 7.8 2.0 0:15.73 someprocess
4223 user2 20 0 156208 9928 9124 R 2.0 0.1 0:00.15 anotherprocess
Display Processes by Specific User
Code:
top -u username
Motivation: Often, you need to monitor or debug processes for a particular user, especially in multi-user systems or when working with shared resources. Showing processes for a single user streamlines your ability to troubleshoot user-specific issues or understand the workload they are generating on the system.
Explanation: By using the -u
option followed by a username, ’top’ filters and displays only processes owned by the specified user. This is vital for administrators to narrow down process data to a specific user for security monitoring or resource usage evaluation.
Example output:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1980 username 20 0 133340 2980 2440 S 0.7 0.0 0:12.33 userprocess
2023 username 20 0 156208 9928 9124 S 0.5 0.1 0:19.54 otheruserproc
Sort Processes by Field
Code:
top -o %CPU
Motivation: Sorting processes by specific criteria, such as CPU usage, allows for the quick identification of processes with the biggest impact on system load. When optimizing performance or diagnosing bottlenecks, sorting by a chosen field helps in prioritizing which processes to target for adjustments or reductions in resource consumption.
Explanation: The -o
option is used to dictate which field by which the processes should be sorted. In this example, %CPU
is specified, which sorts processes in descending order based on CPU usage. This directs attention to the processes taking the most processing power at the top of the list.
Example output:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2210 user1 20 0 762660 52848 13056 R 53.0 0.7 0:05.87 heavyprocess
2054 user2 20 0 772768 33744 10888 S 11.8 0.4 0:02.76 lightprocess
Show Individual Threads of a Process
Code:
top -Hp 1234
Motivation: In certain complex applications, multiple threads within a single process can operate independently on different tasks. Monitoring individual threads provides insights into the specific parts of an application that are demanding resources, aiding in debugging and performance tuning.
Explanation: By using -H
with -p
followed by a process ID, you can see all threads associated with a specific process. -H
displays all threads, while -p
takes an individual process ID allowing you to isolate and inspect the threads of that particular process.
Example output:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 user1 20 0 1032444 35088 15208 S 3.2 0.4 0:02.33 thread_1
1235 user1 20 0 1032444 35088 15208 S 1.5 0.4 0:01.29 thread_2
1236 user1 20 0 1032444 35088 15208 S 0.8 0.4 0:00.78 thread_3
Show Only Processes with Given PID(s)
Code:
top -p $(pgrep -d ',' process_name)
Motivation: In environments involving many processes, you might need to focus your monitoring on a select few processes running under a specific name. By targeting particular processes, users can more easily retrieve a concentrated set of data from known applications of interest without wading through extraneous information.
Explanation: This command uses -p
to specify which PIDs to display. $(pgrep -d ',' process_name)
dynamically retrieves the process IDs that match the specified process name and formats them as a comma-separated list which is suitable for -p
. This effectively narrows down the display to the processes of interest.
Example output:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3345 user1 20 0 903432 58800 18456 S 9.7 0.8 0:07.44 processname
3541 user1 20 0 982640 69200 22280 S 7.4 1.0 0:05.22 processname
Display Help about Interactive Commands
Code:
?
Motivation: While using ’top,’ users may need to alter or maximize functionality through interactive commands. Providing a quick reference within the tool increases efficiency, enabling users to leverage the ’top’ command’s full potential without needing external documentation.
Explanation: Typing ?
while ’top’ is running reveals the help menu within the terminal. This menu details usable keyboard shortcuts and options for real-time interface tweaks such as field sorting, renaming, or toggling visible fields, enhancing usability and versatility.
Example output:
h: Toggle summary display k: Kill a task
n: Set maximum tasks displayed d: Change refresh delay
o, O: Order fields ?: Help with commands
Conclusion
The ’top’ command is an invaluable tool for real-time process monitoring and system management, equipped with numerous options to tailor the display according to specific needs. Whether you are focusing on particular users, measuring resource usage, or wanting to understand an application’s thread behavior, ’top’ accommodates with versatile displays that simplify troubleshooting and performance tuning endeavors. Leveraging these use cases can significantly enhance system administration efficiency and precision.