Introduction

You have probably noticed your Linux OS slowing down, especially when working harder. Understanding CPU processor usage is critical for overall performance measurement.

CPU stands for Central Processing Unit, or processor. The processor is the chip responsible for all the calculations done by a computer. Higher-end processors can perform more calculations, simultaneously, and more quickly.

It makes sense that you’d want a toolset to monitor CPU usage in Linux from the command line. This guide will walk you through several options to check CPU usage for Linux-based systems.

Prerequisites

  • A Linux-based computer (Ubuntu and CentOS are two examples)
  • Access to a user account with sudo privileges

Tools/Software

  • A command prompt (Ctrl-Alt-T in Ubuntu, Menu > Applications > Utilities > Terminal in CentOS)
  • (optional) A package installer, like apt or yum, usually included by default

NOTE

To get the memory usage of only a particular application (as opposed to the shared libraries it uses), use the Linux smaps interface.

How To Check CPU Usage From Linux Command Line

top command to view Linux CPU load

Open a terminal window and enter the following:

top

The system should respond by displaying a list of all the processes that are currently running.  It will also give a readout of users, tasks, CPU load, and memory usage.

This list can change frequently, as background tasks start and complete.  One helpful switch is to launch top with the –i switch:

top –i

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

To quit the top function, press the letter q on your keyboard.

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, you can press the letter h while it’s running. Or, you can enter the following at a command line:

man top

This will display the manual page for the top command.

mpstat Command To Display CPU Activity

Mpstat is part of a software bundle called sysstat. Most RedHat (and RHEL) based systems included the software package.

For Debian (and Ubuntu) systems, you’ll need to install the sysstat package.

In a terminal window, enter the following:

sudo apt-get install sysstat

Allow the process to complete.

If you’re running an older (4.x or older) version of CentOS or Red Hat derivative, you can use up2date to install sysstat:

sudo up2date install sysstat

For newer (5.x and later) installations of CentOS or Red Hat, sysstat can be installed using the following:

sudo yum install sysstat

Once the process finishes, you can use the mpstat command in the terminal as follows:

mpstat

The system will display usage for each processor (or processor core).

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 handing 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)

You can add switches to the mpstat command.

The –P switch allows you to specify a single processor to report:

mpstat –P 0

This would show you a report for the first processor (CPU 0).

mpstat –P ALL

This command would show you the total, like the basic mpstat command. It will also list processes by individual CPU.

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

This example would generate 7 snapshots, each 5 seconds apart.

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.

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

sar –u 5

The –u option tells it to display CPU usage. The 5 indicates that it should display every 5 seconds.  This will run indefinitely ,  to cancel, press Ctrl-C.

iostat Command For Average Usage

In a terminal, enter the following:

iostat

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

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

Other Options To Monitor CPU Performance

Nmon Monitoring Tool

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

sudo apt-get install nmon

To install to CentOS, enter the following:

sudo yum epel-release

sudo yum install nmon<

The command to launch nmon is:

Nmon

This will launch the utility, and display all the options. To view CPU usage, press the letter c. To switch back, press c again. 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.

Note: The Ubuntu 18.04 does not include a GUI. However, the standard version of Ubuntu uses the gnome graphical interface. Depending on your preferences, there are many lightweight GUI’s available for installation.

Typically, GUI’s 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. These commands will help you track processor usage and performance of your system, giving you greater control.

You can also install third-party applications, or even use graphical utilities.