In this guide, we’ll go over a few methods to check and monitor the CPU utilization on a Linux system. Whether you are in charge of a server or just your personal desktop, the computer’s CPU usage is useful information that’s easy to acquire.
In this tutorial you will learn:
- How to check CPU usage with top
- Understanding the output from top and htop
- Monitor CPU usage with systat package
- How to configure CPU monitoring alters
Software Requirements and Conventions Used
|Category||Requirements, Conventions or Software Version Used|
|System||Ubuntu, Debian, CentOS, RHEL, Fedora|
|Other||Privileged access to your Linux system as root or via the |
|Conventions|| # - requires given linux commands to be executed with root privileges either directly as a root user or by use of |
How to check CPU usage with topA great way to check the current CPU usage is with the
topcommand. A lot of the output from this command is rather complex, but it gives very granular information about how the CPU is being utilized on a system.
$ topThis will open up a display in the terminal that has a live view of services running on the system, the amount of system resources each of those services are using, as well as a summary of the system’s CPU utilization, among other information.
topcommand mostly works the same across all Linux distributions, although there are some variants which may display the information a little differently – in a different order, for example.
$ top -vExpected output:
The display window from the top command is not very user friendly at first because of the sheer amount of information and all the terminology and abbreviations used. We’ll cover everything you need to know below, so you can interpret the data from top.
The first line shows (in order): system time, system uptime (how long since last reboot), number of active user sessions, and the system’s load average. The load average is particularly relevant for us, as it sheds some light on the system’s CPU usage over time.
That should be easy enough to understand, but you may also see load averages greater than 1.00. This is because load average is not a direct measurement of CPU usage, but how much “work” (load) your system is trying to process. For example, a value of 2.50 means that the current load is 250%, and also indicates that the system is overloaded by a whopping 150%.
The second line of top is pretty self-explanatory and displays the number of tasks running on the system, as well as the current state they’re in.
The third line is where we find our CPU usage, with some detailed statistics that take a little knowledge to interpret.
- us: Percentage of CPU time spent in user space (running user-spawned processes).
- sy: Percentage of CPU time spent in kernel space (running system processes).
- ni: Percentage of CPU time spent on running processes with a user-defined priority (a specified nice value).
- id: Percentage of CPU time spent idle.
- wa: Percentage of CPU time spent on waiting on I/O from hardware. Example: waiting for a hard drive to finish reading data.
- hi: Percentage of CPU time spent processing hardware interrupts. Example: the network card (or any piece of hardware) interrupting the CPU to notify it that new data has arrived.
- si: Percentage of CPU time spent processing software interrupts. Example: a high priority service interrupting the CPU.
- st: Percentage of CPU time that was stolen from a virtual machine. Example: the CPU needed to “steal” resources from a virtual machine in order to process the physical machine’s workload.
Making top simplerSince the top command shows a lot of detailed information, it’s not an ideal method for getting a quick glance at CPU utilization; however,
topgives us a few options to streamline the output and spare some of the complex details.
topis running, you can press the ‘t’ key to cycle through some different views, and get a simpler output of the CPU usage:
htop, which is similar to
topbut geared more towards normal tasks. You can use your package manager to install it.
Ubuntu and Debian:
$ sudo apt-get install htopCentOS and Red Hat:
# yum install htopFedora:
# dnf install htopAfter it’s installed, just type
htopto open it.
htopis more concise and better suited than
topfor simple measuring of CPU usage.
You can exit this screen the same way as top, by pressing ‘q’.
More ways to check CPU utilizationThere are a few more tools we can use to check CPU usage, and they’re contained in the sysstat package. You will have to install this package in order to use the commands.
Ubuntu and Debian:
$ sudo apt-get install sysstatCentOS and Red Hat:
# yum install sysstatOnce the sysstat package is installed, you will have access to the
mpstatcommand. This shows a lot of the same information as
top, but in a concise, one-time output.
user@ubuntu1:~$ mpstat Linux 5.0.0-23-generic (ubuntu1) 01/16/2020 _x86_64_ (1 CPU) 02:31:05 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 02:31:05 AM all 1.41 0.05 0.40 0.04 0.00 0.00 0.00 0.00 0.00 98.09Another command that comes with this package is
sar. It’s most useful when coupled with a number in the command. This allows you to specify how often (in seconds) the
sarcommand should output information about CPU utilization.
For example, to check CPU usage every 4 seconds:
$ sar 4The output will look like this, and output a new line every 4 seconds:
user@ubuntu1:~$ sar 4 Linux 5.0.0-23-generic (ubuntu1) 01/16/2020 _x86_64_ (1 CPU) 02:33:24 AM CPU %user %nice %system %iowait %steal %idle 02:33:25 AM all 9.09 0.00 0.00 0.00 0.00 90.91To make
sarstop after a certain number of outputs, specify a second number. For example:
$ sar 2 5This will make
sarcheck the CPU usage every 2 seconds, 5 times. It will also show the average of all 5 of its probes at the end of the output.
Graphical monitoringIf you are using a desktop client or have a GUI installed, there should be a graphical tool for monitoring system usage. Ubuntu uses Gnome by default as its desktop environment, and the command to launch the system manager is:
$ gnome-system-monitorThis will open a window similar to Window’s task manager, where you can sort processes by their CPU usage. Other distributions and desktop environments should have a similar tool.
How to configure monitoring alertsThere are a lot of different ways to code a script that monitors CPU usage. In this part of the guide, we’ll go over one possible script where CPU usage is monitored every minute, and we’ll configure it to send an email when CPU usage gets high.
This script uses sed to grab the average CPU idle percentage from
#!/bin/bash CPU=$(sar 1 5 | grep "Average" | sed 's/^.* //') CPU=$( printf "%.0f" $CPU ) if [ "$CPU" -lt 20 ] then echo "CPU usage is high!" | sendmail firstname.lastname@example.org fi
sar. Then, it uses an if function to check if the idle percentage is below a certain number, and will send an email to the administrator if it is. In this case, it’s configured for 20% - in other words, if CPU usage is beyond 80%, the administrator gets an email.
The script can be tweaked as needed, like if you want it to echo a warning to the terminal or record to a log file instead of sending an email with
Of course, you’d need to call this script from cron if you want it to run routinely.
$ crontab -eTo run it every minute, you’d write this line:
* * * * * /path/to/cpu-alert.sh
ConclusionIn this article, we saw how to check and monitor CPU utilization on a Linux system. We learned about multiple tools that can help us with the monitoring, and also learned how to set up usage alerts so we can be notified when CPU utilization is high.
Using the various methods from this guide, you’ll always know the best tool for keeping tabs on your system usage - whether you need detailed information or just need to quickly see how your system is allocating its CPU.