# How to Check CPU Usage Linux


Checking CPU usage on Linux is something you might need to do often. Whether you want to monitor your system’s performance or troubleshoot slowdowns, knowing how to check CPU usage is essential. You don’t have to be a Linux expert to get this done. I’ll guide you through easy methods that work on almost any Linux system.

In this article, you’ll discover several ways to check CPU usage using built-in commands and tools. I’ll explain how to use them step-by-step, so you can pick the method that suits you best. By the end, you’ll feel confident monitoring your Linux system’s CPU load anytime.

## Understanding CPU Usage on Linux

Before diving into commands, it helps to know what CPU usage means. Your CPU handles all the tasks your computer runs. When many processes demand attention, CPU usage goes up. High CPU usage can slow your system or cause overheating.

CPU usage is usually shown as a percentage. It tells you how much of your CPU’s capacity is being used at a given moment. Linux systems often have multiple CPU cores, so usage can be shown per core or as an average.

Here are some key points about CPU usage:

- **User time:** CPU time spent running user processes.
- **System time:** CPU time spent on system (kernel) tasks.
- **Idle time:** Time when CPU is not doing any work.
- **IO wait:** Time CPU waits for input/output operations.

Knowing these helps you interpret the numbers you see when checking CPU usage.

## Using the top Command to Check CPU Usage

One of the simplest ways to check CPU usage on Linux is the `top` command. It’s installed by default on most distributions and provides a real-time view of system performance.

To use it, open your terminal and type:

```bash
top
```

You’ll see a screen updating every few seconds with a list of running processes and CPU stats at the top.

Look at the first few lines for CPU info:

- `%us` shows user CPU usage.
- `%sy` shows system CPU usage.
- `%id` shows idle CPU percentage.
- `%wa` shows IO wait time.

The processes below are sorted by CPU usage by default, so you can spot which programs use the most CPU.

To exit `top`, press `q`.

### Why use top?

- Real-time monitoring.
- Shows CPU usage per process.
- Easy to use and available everywhere.

## Checking CPU Usage with mpstat

If you want more detailed CPU stats, `mpstat` is a great tool. It’s part of the `sysstat` package, which you might need to install first:

```bash
sudo apt install sysstat   # For Debian/Ubuntu
sudo yum install sysstat   # For CentOS/RHEL
```

Once installed, run:

```bash
mpstat -P ALL 1 1
```

This command shows CPU usage for all cores (`-P ALL`) once (`1 1` means one report after one second).

You’ll get a table with columns like `%usr`, `%sys`, `%idle`, and `%iowait` for each CPU core.

### Benefits of mpstat

- Detailed per-core CPU usage.
- Shows breakdown of CPU time types.
- Useful for diagnosing CPU bottlenecks.

## Using vmstat for CPU and System Stats

`vmstat` is another handy command that shows CPU usage along with memory and IO stats. It gives a quick snapshot of system health.

Run:

```bash
vmstat 1 5
```

This runs `vmstat` every second, five times.

Look at the CPU columns at the end:

- `us` = user CPU time.
- `sy` = system CPU time.
- `id` = idle CPU time.
- `wa` = IO wait.

`vmstat` is useful when you want to monitor CPU alongside other system resources.

## Checking CPU Usage with htop for Interactive Monitoring

If you prefer a more user-friendly interface, `htop` is a great alternative to `top`. It shows CPU usage in color and supports mouse interaction.

To install `htop`:

```bash
sudo apt install htop   # Debian/Ubuntu
sudo yum install htop   # CentOS/RHEL
```

Run it by typing:

```bash
htop
```

You’ll see CPU usage bars for each core at the top. Processes are listed below with their CPU and memory usage.

`htop` lets you sort processes, kill tasks, and customize views easily.

### Why choose htop?

- Colorful, easy-to-read display.
- Per-core CPU usage bars.
- Interactive process management.

## Using the /proc/stat File for Raw CPU Data

Linux keeps CPU stats in the `/proc/stat` file. You can read this file to get raw CPU usage data.

Run:

```bash
cat /proc/stat | grep '^cpu '
```

You’ll see a line like:

```
cpu  123456 7890 23456 987654 1234 0 0 0 0 0
```

These numbers represent time spent by the CPU in different modes since boot, measured in jiffies (units of time).

To calculate CPU usage from this data, you need to:

- Read the values twice, a short time apart.
- Calculate the difference in idle and total time.
- Compute the percentage of CPU used.

This method is more advanced but useful for scripting or custom monitoring.

## Using the sar Command for Historical CPU Usage

Sometimes, you want to check CPU usage history, not just real-time stats. The `sar` command from the `sysstat` package helps with this.

Install `sysstat` if needed:

```bash
sudo apt install sysstat
```

Enable data collection by editing `/etc/default/sysstat` and setting `ENABLED="true"`, then restart the service:

```bash
sudo systemctl restart sysstat
```

Run:

```bash
sar -u 1 5
```

This shows CPU usage every second for five intervals, including user, system, idle, and IO wait times.

`sar` is great for tracking CPU trends over time.

## Graphical Tools to Check CPU Usage on Linux

If you prefer graphical interfaces, many Linux desktop environments include system monitors.

- **GNOME System Monitor:** Shows CPU usage graphs and process lists.
- **KDE System Monitor:** Offers detailed CPU and resource charts.
- **Xfce Task Manager:** Lightweight CPU usage display.

These tools are easy to use and good for casual monitoring.

## Tips for Monitoring CPU Usage Effectively

To get the most from your CPU monitoring, keep these tips in mind:

- Use real-time tools like `top` or `htop` for quick checks.
- Use `mpstat` or `sar` for detailed or historical data.
- Monitor per-core usage to spot uneven load.
- Combine CPU monitoring with memory and disk IO checks.
- Automate monitoring with scripts if needed.

## Troubleshooting High CPU Usage

If you notice high CPU usage, here’s what you can do:

- Identify the process using the most CPU with `top` or `htop`.
- Check if the process is necessary or stuck.
- Restart or kill problematic processes.
- Look for system updates or bugs causing CPU spikes.
- Consider upgrading hardware if CPU is consistently overloaded.

## Conclusion

Now you know several ways to check CPU usage on Linux, from simple commands like `top` to detailed tools like `mpstat` and `sar`. Whether you want a quick glance or deep analysis, Linux offers many options.

By regularly monitoring CPU usage, you can keep your system running smoothly and catch problems early. Try out these commands and tools to find the best fit for your needs. With a bit of practice, checking CPU usage will become second nature.

### FAQs

### How do I check CPU usage for a specific process in Linux?

You can use `top` or `htop` and look for the process by name or PID. These tools show CPU usage per process in real time.

### What does high IO wait mean in CPU usage?

High IO wait means the CPU is waiting for disk or network operations to complete. It can slow down your system if excessive.

### Can I monitor CPU usage over time on Linux?

Yes, use the `sar` command from the `sysstat` package to collect and view historical CPU usage data.

### Is there a graphical tool to check CPU usage on Linux?

Yes, GNOME System Monitor, KDE System Monitor, and Xfce Task Manager provide graphical CPU usage displays.

### How often should I check CPU usage on my Linux server?

It depends on your needs. For critical servers, continuous monitoring with tools like `htop` or automated scripts is best. For casual use, occasional checks are fine.
