This is not your typical boring tutorial where you have to read through pages of text and memorize commands like some sort of robot. Nope, this one will be different.
To set the stage, let’s open up a terminal window (if you don’t know how to do that, shame on you!) and type in “top”. You should see something like this:
# This script displays system information using the "top" command.
# First, we need to open a terminal window and type in "top".
# This will show us the current processes and system usage.
top
# The output will include the following information:
# - The current time (18:20:35)
# - The system uptime (4 days, 7:39)
# - The number of users (1)
# - The system load average (0.60, 0.50, 0.40)
# - The number of tasks (190)
# - The number of running tasks (1)
# - The number of sleeping tasks (189)
# - The number of stopped tasks (0)
# - The number of zombie tasks (0)
# Next, we will see the CPU usage breakdown:
# - The percentage of CPU time used by user processes (2.3%)
# - The percentage of CPU time used by system processes (0.7%)
# - The percentage of CPU time used by processes with a priority level of "nice" (0.0%)
# - The percentage of CPU time spent idle (96.9%)
# - The percentage of CPU time spent waiting for input/output (0.0%)
# - The percentage of CPU time spent handling hardware interrupts (0.0%)
# - The percentage of CPU time spent handling software interrupts (0.0%)
# - The percentage of CPU time stolen by virtual machines (0.1%)
%Cpu(s): 2.3 us, 0.7 sy, 0.0 ni, 96.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.1 st
# Then, we will see the memory usage breakdown:
# - The total amount of physical memory (845M)
# - The amount of physical memory currently in use (78M)
# - The amount of physical memory currently free (767M)
# - The amount of physical memory used for buffering and caching (32M)
KiB Mem : 845M total, 78M used, 767M free, 32M buff/cache
# Finally, we will see the swap usage breakdown:
# - The total amount of swap space (1639M)
# - The amount of swap space currently in use (0M)
# - The amount of swap space currently free (1639M)
# - The amount of swap space used for caching (100M)
KiB Swap: 1639M total, 0M used, 1639M free, 100M cached
This is the default view of top. It shows you a list of running processes and some system information like CPU usage, memory usage, etc. Let’s dig into this at what each section means:
– “Tasks” This tells us how many tasks (processes) are currently running on our system. In this case, we have 190 total tasks with only one of them being active right now. The other 189 processes are sleeping or stopped.
– “%Cpu(s)” This shows the CPU usage over time in a percentage format. Here we can see that about 2.3% of our CPU is currently used by user space, while 0.7% is being used for system tasks (kernel). The rest of the CPU is idle and waiting to be used.
– “KiB Mem” This shows us how much memory we have in total, how much is currently being used, and how much is free. In this case, we have 845MiB of RAM with only 78MiB being used at the moment. The rest (767MiB) is available for use by our processes.
– “KiB Swap” This shows us how much swap space we have in total, how much is currently being used, and how much is free. In this case, we have 1639MiB of swap with none of it being used at the moment (which is good!).
Now some useful commands that you can use while top is running:
– “q” This will quit top and return you to your terminal.
– “r” This will refresh the display with updated information. You don’t need to press this every time, as it automatically refreshes every few seconds by default.
– “k PID” This will kill a process with the specified PID (process ID). For example, if you want to kill a process with PID 12345, type in “k 12345”.
– “f” This will toggle the display of command names instead of program names. By default, top shows program names like “firefox” or “chrome”, but if you prefer to see the actual commands that are being executed (like “bash”), type in “f”.
– “d NUM” This will change the delay time between refreshes. The default is 3 seconds, but you can increase it by typing in a larger number like “d 5” for a 5 second refresh rate.
– “o FIELD” This will sort processes based on a specific field (like CPU usage or memory usage). For example, if you want to see the top 10 processes with the highest CPU usage, type in “o %Cpu(s)”.
– “p PID1 [PID2]…” This will filter the display to show only processes with specific IDs. For example, if you want to see only the processes with PIDs 12345 and 67890, type in “p 12345 67890”.
– “u USER” This will filter the display to show only processes owned by a specific user. For example, if you want to see only the processes owned by user “johndoe”, type in “u johndoe”.
That’s it for now! We hope this tutorial was helpful and that you learned something new about top command for process management on Linux systems. Remember, practice makes perfect so keep using top and experimenting with different commands until you feel comfortable with it.