As a Linux administrator, it’s important to keep an eye on how your server (or servers) is performing. One way to measure its performance is to track the CPU usage. This will give you insight into the performance of the system as well as show how the hardware resources are being divided up across the various running services.

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
How to Check and Monitor CPU utilization on Linux
How to Check and Monitor CPU utilization on Linux

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu, Debian, CentOS, RHEL, Fedora
Software Apache Webserver
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

How to check CPU usage with top

A great way to check the current CPU usage is with the top command. 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.
$ top
This 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.
Checking the current CPU usage with top command
Checking the current CPU usage with top command.
NOTE
The top command 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.
The top program installed on Ubuntu systems comes from the procps-ng package, and is the one we will be working with in this article. To check what variant your system is running, use the -v flag with top:
$ top -v
Expected output:
procps-ng 3.3.12

SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


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.
Load average
Load average
There are three numbers given for the load average. The numbers are the average load over 1, 5, and 15 minutes, respectively. Think of these numbers as percentages – a load of 0.2 means 20%, and a load of 1.00 means 100%.

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.
CPU usage from top
CPU usage from top
  • 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.
The next two lines are dedicated to memory information, and aren’t relevant for monitoring CPU usage. Below that, there’s a list of running processes and a column titled %CPU, which contains the current CPU usage of each process listed.
CPU usage percentage
CPU usage percentage
This gives a quick view of which services are chewing up the most CPU resources. You can quit top any time by pressing ‘q’.

Making top simpler

Since the top command shows a lot of detailed information, it’s not an ideal method for getting a quick glance at CPU utilization; however, top gives us a few options to streamline the output and spare some of the complex details.

While top is running, you can press the ‘t’ key to cycle through some different views, and get a simpler output of the CPU usage:
CPU usage graph
CPU usage graph
Another option is to use htop, which is similar to top but geared more towards normal tasks. You can use your package manager to install it.

Ubuntu and Debian:
$ sudo apt-get install htop
CentOS and Red Hat:
# yum install htop
Fedora:
# dnf install htop
After it’s installed, just type htop to open it.
$ htop
htop command
htop command
As you can see from the screenshot above, the output from htop is more concise and better suited than top for simple measuring of CPU usage.

You can exit this screen the same way as top, by pressing ‘q’.


More ways to check CPU utilization

There 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 sysstat
CentOS and Red Hat:
# yum install sysstat
Once the sysstat package is installed, you will have access to the mpstat command. 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.09
Another 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 sar command should output information about CPU utilization.

For example, to check CPU usage every 4 seconds:
$ sar 4
The 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.91
To make sar stop after a certain number of outputs, specify a second number. For example:
$ sar 2 5
This will make sar check 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 monitoring

If 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-monitor
This 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.
Graphical monitoring with GNOME System Monitor on Ubuntu 20.04 Desktop
Graphical monitoring with GNOME System Monitor on Ubuntu 20.04 Desktop


How to configure monitoring alerts

There 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.
#!/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 admin@example.com
fi
This script uses sed to grab the average CPU idle percentage from 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 sendmail.

Of course, you’d need to call this script from cron if you want it to run routinely.
$ crontab -e
To run it every minute, you’d write this line:
* * * * * /path/to/cpu-alert.sh

Conclusion

In 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.
FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.