If you’re working with Linux, you’re likely already familiar with the terminal and the command line. One of the most important aspects of monitoring your Linux system is keeping an eye on your CPU usage. In this article, we’ll provide a comprehensive guide on how to check Linux CPU usage, including detailed descriptions, code examples, and related concepts.
What is CPU Usage?
Before we dive into how to check CPU usage, let’s first define what CPU usage is. CPU usage refers to the amount of processing power being used by your computer’s central processing unit (CPU). When your computer is running programs or applications, the CPU is responsible for executing the instructions that those programs give. The amount of CPU usage is measured as a percentage of the total processing power available on your computer.
Checking CPU Usage with the Terminal
The most common way to check CPU usage on Linux is by using the terminal. There are several commands you can use to check CPU usage, including top
, htop
, and mpstat
.
Using the top
Command
The top
command is used to display information about the processes running on your system, including CPU usage. To use the top
command, simply open a terminal and type top
:
$ top
This will display a live view of the processes running on your system, sorted by CPU usage. The output will look something like this:
top - 13:50:00 up 1 day, 3:45, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 98 total, 1 running, 97 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8030612 total, 1650716 free, 3657580 used, 2722316 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 3575540 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
297 root 20 0 31360 2540 2156 R 0.3 0.0 0:00.10 top
1 root 20 0 225312 13768 9620 S 0.0 0.2 0:05.86 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.03 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
The %CPU
column shows the CPU usage percentage for each process. In this example, the top
process itself is using 0.3% of the CPU.
Using the htop
Command
The htop
command is similar to top
, but with a more user-friendly interface. To use htop
, simply open a terminal and type htop
:
$ htop
This will display a live view of the processes running on your system, sorted by CPU usage. The output will look something like this:
The %CPU
column shows the CPU usage percentage for each process, just like in top
.
Using the mpstat
Command
The mpstat
command is used to display CPU usage statistics for each processor in a multi-processor system. To use mpstat
, simply open a terminal and type mpstat
:
$ mpstat
This will display CPU usage statistics for each processor in your system. The output will look something like this:
Linux 5.11.0-27-generic (hostname) 08/25/2021 _x86_64_ (8 CPU)
12:19:31 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
12:19:31 PM all 0.31 0.00 0.06 0.00 0.00 0.00 0.00 0.00 0.00 99.63
12:19:31 PM 0 0.25 0.00 0.05 0.00 0.00 0.00 0.00 0.00 0.00 99.70
12:19:31 PM 1 0.05 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 99.94
12:19:31 PM 2 0.20 0.00 0.05 0.00 0.00 0.00 0.00 0.00 0.00 99.75
12:19:31 PM 3 0.15 0.00 0.03 0.00 0.00 0.00 0.00 0.00 0.00 99.82
12:19:31 PM 4 0.22 0.00 0.05 0.00 0.00 0.00 0.00 0.00 0.00 99.72
12:19:31 PM 5 0.05 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 99.94
12:19:31 PM 6 0.13 0.00 0.03 0.00 0.00 0.00 0.00 0.00 0.00 99.84
12:19:31 PM 7 0.06 0.00 0.01 0.00 0.00 0.00 0.00 0.00 0.00 99.93
The %idle
column shows the percentage of time that each processor is idle.
Conclusion
In this article, we’ve provided a comprehensive guide on how to check Linux CPU usage using the terminal. We’ve covered three commands that can be used to monitor CPU usage: top
, htop
, and mpstat
. By using these commands, you can keep an eye on your CPU usage and ensure that your system is running smoothly.
Remember that CPU usage is just one aspect of system monitoring. It’s important to monitor other aspects of your system as well, such as memory usage, disk usage, and network activity. By keeping a close eye on your system, you can identify issues before they become major problems.