How to Check CPU Utilization in Linux with Command Line

Introduction

CPU utilization in Linux is an important metric to monitor when managing a Linux system. It is important to know how much of the CPU is being used in order to ensure that the system is running efficiently and that there is enough capacity to handle additional workloads. Fortunately, there are several command line tools available in Linux that can be used to check CPU utilization. In this article, we will discuss how to check CPU utilization in Linux using the command line. We will cover the different commands and tools available, as well as how to interpret the output.

How to Check CPU Utilization in Linux with Command Line

1. Use the top command

The top command is the traditional way to view your system’s resource usage and see the processes that are taking up the most system resources. To view the CPU utilization, type the following command:

top

2. Use the mpstat command

The mpstat command can be used to generate reports that can be used to show the utilization of each CPU individually. To view the CPU utilization, type the following command:

mpstat

3. Use the sar command

The sar command can be used to monitor system performance and report CPU utilization. To view the CPU utilization, type the following command:

sar -u

4. Use the ps command

The ps command can be used to view the CPU utilization of individual processes. To view the CPU utilization, type the following command:

ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10

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.

Article on how to check CPU utilization in Linux from the terminal.

Prerequisites

  • A Linux-based computer (e.g., Ubuntu and CentOS)
  • Access to a user account with sudo privileges
  • 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

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 frequently change, 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 RHEL-based distributions include this 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 command:

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.

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 or third-party applications. These commands will help you track processor usage and performance of your system, giving you greater control.

How to Check CPU Utilization in Linux with Command Line

Checking CPU utilization in Linux is an important task for system administrators. Knowing how much CPU is being used at any given time can help you determine if your system is running efficiently or if it needs to be optimized. Fortunately, there are several command line tools that can help you check CPU utilization in Linux.

1. top Command

The top command is the most commonly used command for checking CPU utilization in Linux. It provides a dynamic real-time view of the running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel. To use the top command, simply type “top” at the command prompt.

2. mpstat Command

The mpstat command is used to display individual CPU utilization. It can be used to monitor individual CPU performance as well as aggregate CPU utilization across all CPUs. To use the mpstat command, type “mpstat” at the command prompt.

3. sar Command

The sar command is used to collect, report, and save system activity information. It can be used to monitor CPU utilization over a period of time. To use the sar command, type “sar” at the command prompt.

4. pidstat Command

The pidstat command is used to monitor individual processes and report their CPU utilization. It can be used to monitor individual processes as well as aggregate CPU utilization across all processes. To use the pidstat command, type “pidstat” at the command prompt.

5. iostat Command

The iostat command is used to report CPU utilization as well as input/output statistics for devices and partitions. It can be used to monitor individual devices as well as aggregate CPU utilization across all devices. To use the iostat command, type “iostat” at the command prompt.

Conclusion

Checking CPU utilization in Linux is an important task for system administrators. Fortunately, there are several command line tools that can help you check CPU utilization in Linux. The top, mpstat, sar, pidstat, and iostat commands are all useful for monitoring CPU utilization in Linux.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?