Monitor process details with top command
In this step, you will learn about the top
command, which provides a dynamic, real-time view of the processes running on your system. While ps aux
gives you a snapshot, top
continuously updates, showing you which processes are using the most CPU and memory.
The top
command is very useful for monitoring system performance and identifying processes that might be consuming excessive resources.
Type the following command in your terminal and press Enter:
top
Your terminal will change to display a constantly updating list of processes. The output looks similar to this:
top - HH:MM:SS up X days, HH:MM, X users, load average: X.XX, X.XX, X.XX
Tasks: XXX total, X running, XXX sleeping, X stopped, X zombie
%Cpu(s): X.X us, X.X sy, X.X ni, XX.X id, X.X wa, X.X hi, X.X si, X.X st
MiB Mem : XXXX.X total, XXXX.X free, XXXX.X used, XXX.X buff/cache
MiB Swap: XXXX.X total, XXXX.X free, XXXX.X used. XXXX.X avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
12345 labex 20 0 123456 54321 32100 S 0.1 0.5 0:00.10 zsh
12367 labex 20 0 21000 5000 3000 R 0.0 0.1 0:00.05 top
...
Let's look at the key areas of the top
output:
-
Summary Area (Top): This section shows system summary information, including:
- Current time and system uptime.
- Number of logged-in users.
- Load average (average number of processes waiting to run over the last 1, 5, and 15 minutes).
- Total number of tasks (processes) and their states (running, sleeping, stopped, zombie).
- CPU usage breakdown (user, system, idle, etc.).
- Memory usage (total, free, used, buffer/cache).
- Swap space usage.
-
Process List Area (Bottom): This section lists individual processes, sorted by default by CPU usage. The columns are similar to ps aux
, but top
updates them in real-time. Key columns include:
PID
: Process ID.
USER
: Owner of the process.
%CPU
: CPU usage percentage.
%MEM
: Memory usage percentage.
COMMAND
: The command name.
While top
is running, you can interact with it using various keys:
- Press
q
to quit top
.
- Press
M
to sort the process list by memory usage.
- Press
P
to sort the process list by CPU usage (this is the default).
- Press
k
to kill a process (you will be prompted for the PID). Be careful with this!
Spend a moment observing the processes and how the CPU and memory usage changes. You'll see the top
process itself near the top of the list because it's actively using CPU to update the display.
When you are finished exploring top
, press q
to exit and return to your regular terminal prompt.
Click Continue to complete this step and the lab.