How to Use the Command 'top' (with examples)
- Osx
- December 17, 2024
The top
command is a powerful utility found in many Unix-like operating systems. It provides real-time, dynamic information about the system’s running processes. This includes various metrics such as CPU usage, memory usage, and process IDs, offering an invaluable tool for system administrators and developers to monitor and manage system performance. By understanding how to customize and interact with the top
command through various flags and options, users can gain deeper insights into system operations.
Use Case 1: Start top
, All Options Are Available in the Interface
Code:
top
Motivation:
Starting top
without any parameters is a simple yet effective way for users to quickly visualize what’s happening on their system. It serves as a baseline view of processes, allowing users to easily identify system performance issues, like high CPU or memory consumption, at a glance. This version of the command provides an unfiltered look at all processes, enabling users to manually explore and apply various interactive commands within the top
interface.
Explanation:
The command top
without additional flags launches the utility with a default configuration that displays a real-time view of all active processes. Users can observe statistics such as process IDs, user ownership, CPU and memory usage, and the command each process is executing. Within this view, key-stroke based options allow users to modify their session interactively to meet their specific analytic needs.
Example Output:
top - 14:32:53 up 1 day, 3:11, 1 user, load average: 0.15, 0.20, 0.16
Tasks: 225 total, 1 running, 224 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.6 us, 1.1 sy, 0.0 ni, 95.2 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8065068 total, 3988236 free, 1372392 used, 2704440 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 6170732 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1123 john 20 0 691128 52784 16244 S 9.0 0.7 12:35.96 gnome-terminal
2487 root 20 0 162944 9064 8068 S 0.7 0.1 0:02.09 sshd
559 root 20 0 366308 35944 20460 S 0.3 0.4 5:42.14 Xorg
...
Use Case 2: Start top
Sorting Processes by Internal Memory Size
Code:
top -o mem
Motivation:
Sorting processes by memory size can be particularly beneficial for users who need to manage or troubleshoot memory-intensive applications. By immediately highlighting processes utilizing the most memory, users can pinpoint and address potential inefficiencies or memory leaks, ensuring optimal system stability and performance.
Explanation:
The -o mem
option modifies how top
filters the displayed processes by ordering them according to internal memory usage. The -o
flag specifies the key by which results will be sorted, and mem
is the indicator for memory size. As a result, processes hogging significant memory resources will be presented at the top of the list, providing immediate visibility.
Example Output:
top - 14:36:40 up 1 day, 3:15, 1 user, load average: 0.34, 0.28, 0.20
Tasks: 225 total, 1 running, 224 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.1 us, 0.5 sy, 0.0 ni, 97.3 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8065068 total, 3988236 free, 1372392 used, 2704440 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 6170732 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
980 mysql 20 0 2120196 136200 16808 S 4.6 1.7 42:01.48 mysqld
3215 john 20 0 9027288 132780 11384 S 1.7 1.6 56:43.75 chrome
249 john 20 0 6961084 121688 24844 S 2.0 1.5 10:04.86 java
...
Use Case 3: Start top
Sorting Processes First by CPU then by Running Time
Code:
top -o cpu -O time
Motivation:
By sorting processes first by CPU usage and then by running time, users gain the ability to identify processes that are both CPU-intensive and long-running, which may be indicative of resource-heavy batch processes or potential runaway tasks. This dual sorting offers a strategic view useful in keeping the system’s CPU resources optimally allocated.
Explanation:
The -o cpu
argument instructs top
to sort the processes by CPU usage initially, where CPU-intensive tasks will appear at the top. Adding -O time
allows for secondary sorting by runtime, potentially revealing processes that monopolize CPU cycles over lengthy durations. Together, these options paint a comprehensive picture of critical resource usage.
Example Output:
top - 14:38:40 up 1 day, 3:20, 1 user, load average: 0.34, 0.28, 0.20
Tasks: 225 total, 1 running, 224 sleeping, 0 stopped, 0 zombie
%Cpu(s): 65.8 us, 14.6 sy, 0.0 ni, 19.3 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8065068 total, 3988236 free, 1372392 used, 2704440 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 6170732 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
843 john 20 0 11909M 83444 34232 R 154.6 1.0 512:32.73 python
2645 root 20 0 162944 9064 8068 S 0.7 0.1 147:02.09 sshd
7998 john 20 0 1958920 25644 16864 S 0.5 0.3 79:49.40 java
...
Use Case 4: Start top
Displaying Only Processes Owned by a Given User
Code:
top -user user_name
Motivation:
When a user needs to monitor only their own processes or track specific user activity, filtering processes owned by a particular user can significantly reduce clutter and improve focus. This approach is particularly useful for developers or administrators managing multi-user systems who wish to isolate the resource usage patterns of specific accounts.
Explanation:
The -user
flag limits the display to include only processes associated with a specified user, designated here as user_name
. This filtering is invaluable for troubleshooting individual user issues or analyzing how specific application instances (tied to user accounts) are interacting with system resources.
Example Output:
top - 14:40:40 up 1 day, 3:21, 1 user, load average: 0.20, 0.24, 0.19
Tasks: 12 total, 0 running, 12 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.6 us, 1.1 sy, 0.0 ni, 95.2 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8065068 total, 3988236 free, 1372392 used, 2704440 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 6170732 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1123 john 20 0 691128 52784 16244 S 9.0 0.7 12:35.96 gnome-terminal
2487 john 20 0 1693784 132960 11234 S 0.6 1.4 0:54.32 chrome
...
Use Case 5: Display Help About Interactive Commands
Code:
?
Motivation:
Understanding the interactive commands available within top
is crucial for any user wishing to master this utility. Displaying the help menu provides an overview of these interactive options, enabling users to manipulate the display dynamically—sorting and filtering processes in real-time to suit their needs.
Explanation:
Entering ?
while top
is running brings up the help menu, which lists all interactive commands available to the user. This aids users in exploring functionalities like changing sorting fields, filtering by specific criteria, or even killing processes directly from within top
.
Example Output:
Help for interactive commands:
U - Show all users
P - Sort by CPU usage
N - Sort by PID
M - Sort by Memory usage
T - Sort by running time
K - Kill a process
R - Renice a process
etc...
Conclusion
The top
command is a versatile tool that can be fine-tuned to fit a variety of monitoring and system administration tasks. By altering its default behavior through the use of flags and options, users can focus on specific system metrics or drill down into individual processes to troubleshoot, optimize, and maintain system health. Its dynamic nature and rich feature set make top
an indispensable command-line tool for both novice and seasoned Unix-like system users.