# How To Check Linux CPU Usage

> Check out our guide to learn how to gather information about the CPU, from current CPU utilization to historical data via the command line.

- Author: [CJ Saathoff](https://www.namehero.com/blog/author/cj-saathoff/)
- Published: 2024-04-17
- Updated: 2024-11-06
- Category: [Linux Command Line](https://www.namehero.com/blog/category/linux-command-line/)
- Web page: https://www.namehero.com/blog/how-to-check-linux-cpu-usage/

[![](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/03/12091346/how-to-check-1024x576.jpg)](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/03/12091346/how-to-check.jpg)

The CPU or central processing unit is a hardware component of a computer that carries out instructions from software. The CPU can be thought of as the computer brain. So as you can guess complete utilization of the CPU resources means that everything has to wait for spare CPU cycles.

In this blog post, we will look into how to gather information about the CPU also known as the processor, from current CPU utilization to historical data on a Linux system via the command line.

## Gathering Context

Before we can make sense of the CPU usage it is best to check CPU information to gather context about how many processors, cores, and threads the system has. Below is a command that will list the information.

```

lscpu | grep -h "Thread(s)\|Core(s)\|Socket(s)"

```

The output will look something like this.

>

```

Thread(s) per core:                 2
Core(s) per socket:                 4
Socket(s):                          1

```

- **Thread(s)**: tells us the threads per core.
- **Core(s)**: tells us the number of cores per processor.
- **Socket(s)**: tells us how many processors there are.

Note: If you need more details like the model name of the processor you can use lscpu without the grep as shown above.

#### More Than One Thread Per Core.

If you see more than one thread more core this is likely the work of hyperthreading or a simmular technology, which allows for virtual cores on a single physical core.

graphical utility options

## Monitor CPU Usage From the Command Line

The below section will go over some examples of Linux commands that can be used to monitor the current CPU usage.

### Using The PS Command

The ps command is part of gnu coreutils, which lists the process ID, CPU usage, and the command.

```

ps -eo pid,pcpu,comm

```

The output from the above command would then look like the following though longer

>

```

    PID %CPU COMMAND
      1  0.0 systemd
      2  0.0 kthreadd
      3  0.0 rcu_gp
      4  0.0 rcu_par_gp
      5  0.0 slub_flushwq
      6  0.0 netns
      8  0.0 kworker/0:0H-events_highpri
     11  0.0 mm_percpu_wq

```

### Using The Top Command

The top command defaults to using CPU usage in most cases.

```

top

```
 ![Screenshot of the top command](https://images.surferseo.art/4044cdfc-591b-45a6-b076-625c9abe17f5.png)

### Htop

The htop command line tool is simmular to the top command and is aimed at making things more human-readable. It should be noted that that is not the [official reason](https://htop.dev/faq.html) for the h in though it isn’t a bad way to remember the difference.

#### To Install

In most Linux distributions you can install the package through the package manager the package name will be htop for most.

The installation docs are listed on the official [website](https://htop.dev/downloads.html#binaries).

Once installed you run the following command

```

htop

```
 ![Screenshot of htop command](https://images.surferseo.art/2dff82ec-6f33-42d0-a7d3-fd75b3254661.png)

## Historical CPU Usage Statistics

If you want to look at historical CPU usage will need to have a method of logging the data ahead of time. For now, let’s focus on a simple solution to gather that data.

### The Sar Command

The Sar command is the simplest way to gather historical system data. It gathers includes CPU, Memory usage, Disk usage, and load to name a few. It gathers this data once every 10 minutes and then when you run the command displays the information in a table.

#### To Install

For Debian and Ubuntu systems

```

sudo apt install sysstat

```

For RHEL and derivatives

```

sudo dnf install sysstat

```

#### To Start And Enable

To make sure it is installed and started run

```

sudo systemctl enable sysstat &amp;&amp; sudo systemctl start sysstat

```

#### To Check

To Check the starts in sar run the following command

```

sar

```

The Output will look something like the following

>

```

11:00:00 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
11:10:00 AM     all      0.29      0.00      0.08      0.28      0.00     99.35
11:20:04 AM     all      0.28      0.00      0.09      2.49      0.00     97.14
11:30:06 AM     all      0.11      0.00      0.08      0.20      0.00     99.61
11:40:00 AM     all      0.29      0.00      0.09      2.51      0.00     97.11
11:50:10 AM     all      0.28      0.00      0.09      0.08      0.00     99.56
12:00:09 PM     all      0.10      0.00      0.07      2.41      0.00     97.42
12:10:07 PM     all      0.29      0.00      0.09      0.42      0.00     99.21
12:20:15 PM     all      0.28      0.00      0.09      2.00      0.00     97.64
12:30:10 PM     all      0.20      0.00      0.09      1.41      0.00     98.30
Average:        all      0.24      0.00      0.09      1.55      0.00     98.13

```

- **%user:** is the CPU percentage from user processes
- **%system:** is the CPU percentage from system processes

### Note: About Gathering CPU Usage Data From Many Servers

If you need data from many servers the general recommended to have a centralized system that either pulls from the servers or is pushed to the centralled collection server.

Using a system like this makes collecting the data quite a bit more complicated, so think carefully before implementing it.

If you decide to go down this route my recommendation is to use promethus. This ends up being made up of a few components the [Prometheus exporter](https://prometheus.io/docs/instrumenting/exporters/), [Prometheus](https://prometheus.io), and the dashboard used to display the data. For CPU usage the exporter you would want to use is the [node exporter](https://github.com/prometheus/node_exporter). To make things easier to read I use [Grafana](https://grafana.com/oss/grafana/) as the dashboard.

Below is how the components connect.

Node Exporter (To Expose) => Prometheus (To Collect) => Grafana (To View)

## Frequency Asked Questions

#### Is The Load Average The Same As CPU Usage?

Another term you may hear when it comes to looking into resource usage is the load average. Be aware that as in the name, average implies there is a calculation being made to generate the load average. This means the the average CPU utilization or usage isn’t directly respresented by the load average.

#### CPU Usage Percentage Is Over 100%?

The CPU utilization percentage of 100% represents the complete utilization of one CPU core. With hyper-threading this means CPU usage of 200% would be using the same as one physical core.

#### How So I Stop A Process ID?

You can follow the [blog post](https://www.namehero.com/blog/the-scroll-of-processes-deciphering-linuxs-kill-command/) on how to kill a process.

## Conclusion

The CPU is a computer’s brain, in this post, we started by discussing how to gather information about the processor using the lscpu command line tool. From there moved on to how to check current CPU usage in various ways. Then touched on historical CPU usage using Sar and went through a few frequency-asked questions

## Related posts

- [How To Check Linux Memory Usage](https://www.namehero.com/blog/how-to-check-linux-memory-usage/)
- [How to Check Open Ports in Linux](https://www.namehero.com/blog/how-to-check-open-ports-in-linux/)
- [How To Check The Status Of Software Raid On Linux](https://www.namehero.com/blog/how-to-check-the-status-of-software-raid-on-linux/)
- [How Do I Check What PCI Devices Are Connected On My Linux System?](https://www.namehero.com/blog/how-do-i-check-what-pci-devices-are-connected-on-my-linux-system/)
