Check CPU usage in Ubuntu
How To

Check CPU usage in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

When you are dealing with a server, resource management is the most crucial and that makes CPU usage monitoring an important task.

But the question is, how do you check the CPU usage in Ubuntu in the first place?

Well, there are multiple ways to do so, but in this tutorial, I will keep things simple and short.

How to check CPU usage in Ubuntu

While there are multiple tools to monitor CPU usage, I intend to list the most popular ones and not bloat your system with its dependencies.

So in this tutorial, I will walk you through the following:

  • Using the top utility
  • Using the mpstat command

1. Using the top utility

You'll find top available in almost every Linux distribution as if used correctly, it can give you tonnes of useful information.

And you can easily start the top utility using the following:

top
use top to check the CPU usage

Take a look at the highlighted part in the above image, it shows the percentage of how much CPU resources are being utilized by a process.

But that's not the best yet.

You can use multiple commands inside the top to get much better  output such as:

Command Description
P Sort processes based on CPU usage
M Sort processes based on Memory usage
T Sort processes based on the run time
N Sort processes based on the process ID

Another tip is to start the top utility with the -i flag which will remove all the processes that are in an idle state that will eventually strip down the list:

top -i
check CPU resources in Ubuntu using top command

2. Using the mpstat command

The mpstat command offers various statistics about CPU utilization which includes user time, idle time, and a lot more.

But unlike top, mpstat does not come pre-installed and for the installation, you'd have to install the sysstat package:

sudo apt install sysstat

Once done, you can start the mpstat using the following:

mpstat
use mpstat command to check the CPU resources

Looks too complex? Let me break it down for you:

  • CPU: Shows the number of specified cores to look for utilization, if specified none, it will show all, a combination of all the cores.
  • %usr: CPU usage in user mode in percentage %.
  • %nice: It indicates the CPU utilization of a user-level process with a nice priority.
  • %sys: CPU usage at the system (kernel) level.
  • %iowait: Shows the percentage of time that the CPU was in an idle state while your system had outstanding disk I/O requests.
  • %irq: Indicates the percentage of time spent by your CPU to service the hardware interrupts.
  • %soft: Percentage of time spent by CPU to service the software interrupts.
  • %steal: Shows the percentage of time spent waiting for a CPU while the hypervisor was servicing the other process.
  • %guest: Indicates the percentage of time used by the CPU to run a virtual process.
  • %gnice: Percentage of time spent by CPU to execute a nice guest.
  • %idle: Shows the percentage of time that the CPU was in an idle state.

If the information is too much for your use case, just focus on two parameters: sys and usr.

Similarly, if you want the same report but for a single core, you can use the -P flag in the following manner:

mpstat -P core_no

For example, here, I went for a report on the 5th core:

use mpstat command to check CPU usage in Ubuntu for specific core

In a related topic, learn about getting GPU info in Ubuntu.

How to Check GPU Info in Ubuntu
Here are various commands to get all kinds of GPU related information in Ubuntu.

Historical CPU usage data

The above two tools show you the CPU consumption at the present time. What if you want to see the trend of CPU utilization over the past 24 hours? In that case, you can use other monitoring tools like atop, s-tui, glances etc.

7 System Monitoring Tools for Linux That are Better Than Top
Top command is good but there are better alternatives. Take a look at these system monitoring tools that are similar to top, but better than it.

You may also use an open source tool like Netdata to monitor your server infrastructure in a graphical way and save the data in the cloud. You can self-host it or use their service for free.

Netdata: Monitoring and troubleshooting transformed
Netdata is a distributed, real-time, performance and health monitoring platform for systems, hardware, containers and applications, collecting thousands of useful metrics with zero configuration needed.

Hope you like this little tutorial.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.