How to Check CPU Utilization in Linux with Command Line

March 6, 2024

Introduction

Understanding CPU processor usage is important for overall system performance measurement. From Linux enthusiasts to system admins, knowing how to monitor CPU utilization in Linux from the command line is crucial.

This guide will walk you through several options to check Linux CPU usage.

Check CPU utilization in Linux from the terminal.

Prerequisites

  • A user account with sudo privileges.
  • Access to a command line/terminal window.
  • A package installer, like apt or dnf, usually included by default.

Note: Use one of 5 available commands in Linux to check memory usage.

How to Check CPU Usage from Linux Command Line

Linux provides a variety of tools for monitoring CPU activity. The following section explains how to use the top, mpstat, sar, and iostat commands to view and interpret CPU usage.

top Command to View Linux CPU Load

Open a terminal window and enter the following command:

top

The system responds by displaying a list of all the processes that are currently running. It also shows users, tasks, CPU load, and memory usage.

Run processes in Linux using the top command.

This list changes frequently as background tasks start and complete. To remedy this, enter top with the -i option:

top -i

The system hides all the idle processes, making it easier to sort through the list.

View only active processes using the top command.

To quit the top function, press q.

Some other useful commands while top is running include:

  • M - sort task list by memory usage.
  • P - sort task list by processor usage.
  • N - sort task list by process ID.
  • T - sort task list by run time.

To get assistance with top, press h while the tool running. Or, you can enter the following at a command line:

man top

This command displays the manual page for the top command.

mpstat Command to Display CPU Activity

Mpstat is part of a software bundle called sysstat. Most RHEL-based distributions include this software package.

For Debian and Ubuntu systems, install the sysstat package using the following command:

sudo apt install sysstat

Once the process finishes, enter the mpstat command:

mpstat

The system displays usage for each processor (or processor core).

The mpstat command columns in Linux.

The first line is a set of column labels. The second line is the value for each column:

  • %usr - % CPU usage at the user level.
  • %nice - % CPU usage for user processes labeled "nice".
  • %sys - % CPU usage at the system (Linux kernel) level.
  • %iowait - % CPU usage idling waiting on a disk read/write.
  • %irq - % CPU usage handling hardware interrupts.
  • %soft - % CPU usage handling software interrupts.
  • %steal - % CPU usage being forced to wait for a hypervisor handling other virtual processors.
  • %guest - % CPU usage spent running a virtual processor.
  • %idle - % CPU usage on idle time (no processes and not waiting on a disk read/write).

Users can add switches to the mpstat command. The -P switch lets you specify a single processor to report:

mpstat -P 0

The system displays a report for the first processor (CPU 0).

mpstats for only one CPU.

Use the ALL option to show total statistics and list processes by individual CPU:

mpstat -P ALL
mpstat shows stats for all CPUs on Linux system.

The mpstat command only takes a snapshot of CPU usage. To take a series of snapshots, use a number to indicate an interval and a second number to indicate the number of reports:

mpstat 5 7
CPU stats recurring in regular intervals using the mpstat command.

The command generates 7 snapshots, each 5 seconds apart, and calculates average values.

sar Command to Show CPU Utilization

The sar tool is a utility for managing system resources. It’s not limited strictly to CPU usage, but you can use the -u option to track CPU performance.

Enter the following command to direct sar to monitor CPU usage at set intervals:

sar -u 5

The -u option displays CPU usage. The 5 indicates that it should display every 5 seconds.

Using the sar command to display CPU statistics in Linux.

The command will run indefinitely. To cancel, press Ctrl+C.

iostat Command for Average Usage

In a terminal window, enter the following:

iostat

The system displays average CPU usage since the last boot. It also displays input/output load (disk read/write activity).

Using the iostat command to display CPU usage in Linux.

More information on iostat can be found on the Linux Manual pages.

Other Options to Monitor CPU Performance

Besides built-in commands for monitoring system performance, Linux also supports advanced third-party tools, like Nmon and GNOME System Monitor. These applications provide graphical interfaces that allow users to analyze CPU usage data and identify potential issues in a user-friendly environment.

Nmon Monitoring Tool

Nmon is a monitoring tool developed by Nigel Griffiths of IBM. To install Nmon on Ubuntu, enter the following command:

sudo apt install nmon

For RHEL-based systems, particularly those using versions 8 and above, you may need to enable the EPEL repository before proceeding with the installation:

sudo dnf install epel-release
sudo dnf install nmon

Launch nmon by typing:

nmon

This launches the utility and displays all the options. To view CPU usage, press the letter c.

Using the nmon tool to display CPU usage in Linux.

For a list of commands, press h. To quit, press q.

Graphical Utility Option

Many server systems don't waste processor cycles on a graphical user interface (GUI). However, you may have a lightweight GUI, or you may be using a client Linux system. Some versions, like Ubuntu, have a built-in graphical monitoring tool.

To launch Ubuntu's system monitor, enter the following in a terminal window:

gnome-system-monitor

This starts a task-manager-like application where you can monitor tasks and CPU usage.

A graphical system monitor in Linux to monitor CPU utilization.

Typically, GUIs have a "task manager" or "system monitor" application. This can be used to monitor CPU usage in real time.

Conclusion

There are many different methods to check CPU usage in Linux.

This guide outlines the primary methods using built-in Linux tools or third-party applications. These commands will help you track processor usage and performance of your system, giving you greater control.

Was this article helpful?
YesNo
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
Next you should read
How to Check Disk Space in Linux
May 8, 2024

This tutorial shows how to display disk usage from a command line in Linux and know how much disk space you have used up, as well as how free disk space you have left.
Read more
Best Tools to Monitor Network Bandwidth on a Linux Server
September 19, 2024

There are many different tools for monitoring network traffic on a Linux server. This guide provides an overview of the best Linux network monitoring tools and their advantages and disadvantages.
Read more
7 Ways to Reduce Server Response Time
December 20, 2023

Reducing server response time t is one of the primary goals of website owners and SEO experts. The first couple of seconds determine whether a client stays on your page or bounces on to the next one.
Read more
Single vs Dual Processor Servers, Which Is Right For You?
November 27, 2024

Learn the differences between a single processor and a dual processor server. Make the best decision for your future infrastructure needs.
Read more