- How to Check CPU Utilization in Linux?
- Checking CPU Utilization Via Command Line?
- Using the top Command
- Using the htop Command
- Using the mpstat Command
- Using the vmstat Command
- Using the sar Command
- Using the iostat Command
- How to Check CPU Utilization Via GUI?
- Conclusion
- How to Check CPU Utilization in Linux with Command Line
- How To Check CPU Usage from Linux Command Line
- top Command to View Linux CPU Load
- mpstat Command to Display CPU Activity
- sar Command to Show CPU Utilization
- iostat Command for Average Usage
- Other Options to Monitor CPU Performance
- Nmon Monitoring Tool
- Graphical Utility Option
How to Check CPU Utilization in Linux?
Monitoring CPU utilization can help you identify which applications use the most resources on your system. Following CPU usage, you can optimize system performance by closing or limiting unnecessary applications. High CPU utilization may indicate that the CPU is struggling to meet the demands of the workload, which can lead to performance issues.
This article will focus on giving you the detailed guideline on how you can check CPU Utilization in Linux by following the content discussed below:
Let’s start the discussion of checking CPU utilization with the command line method.
Checking CPU Utilization Via Command Line?
In this section, we will discuss different commands you can execute to check CPU utilization information.
Using the top Command
The “top” command is an interactive system monitor that displays the CPU utilization and the usage of other system resources in real-time. To view the CPU utilization, press the ‘1’ key while the top is running. This will show the CPU utilization for each individual CPU core.
The CPU section of the top command displays information about the usage of the CPU on the system.
Using the htop Command
The “htop” is an interactive system monitor for Linux that allows you to view and control processes on your system. It displays system resources, including the CPU utilization, memory usage, and load average, in real-time. You need to install this package first by typing the below command and only then you would be able to use it:
After successfully installing it you can run this command by typing:
Different running processes can be seen in the above image and their CPU utilization in terms of percentage can be seen in the “CPU%” field. The “Time+” shows how long that process utilizes the CPU so the high load process can be optimized to improve the system performance.
Using the mpstat Command
The ‘mpstat’ command is a tool that displays the CPU utilization for each individual CPU core. To use it, install the “sysstat” package, which includes tools for collecting and reporting on data such as CPU utilization, memory usage, and network activity.
For other distributions, you can install it using the following commands:
$ sudo dnf install sysstat #For Fedora $ sudo yum install sysstat #For CentOS
After installing this package, you can run the “mpstat” command by typing:
You can see the CPU process-related information in the above image.
Using the vmstat Command
The ‘vmstat’ command is a tool that displays statistics about system memory, processes, and CPU utilization by running the following command:
In the above image, you can see the CPU core information to monitor the system performance and identify which core is causing the performance issues. This will help you identify the core that has a high wait time or high usage so you can take action to optimize the system.
Using the sar Command
In Linux, the sar package is a command-line utility for collecting and displaying system activity information. It collects data about various system resources, including CPU utilization, memory usage, network activity, etc., and stores the data in files located in the /var/log/sysstat directory. The sar command can view the collected data in various formats, such as averages, summaries, and graphs.
You need to enable the sysstat package first to use this command, and you can check its status by typing:
$ systemctl is-enabled sysstat
After that, you need to start and enable the services for the sysstat package by typing:
$ sudo systemctl start sysstat $ sudo systemctl enable sysstat
Now, you can verify the status of the sysstat package again by typing:
$ systemctl is-enabled sysstat
As it is now enabled, you should be able to execute the sar command by typing:
The ‘u’ flag in the above command is mainly used to provide the CPU utilization information whereas this information will be displayed after every 5seconds.
Using the iostat Command
Sysstat also includes a command-line tool called iostat, which is used to monitor CPU utilization and I/O statistics of a system. You can use the iostat command to monitor your system’s performance over time and identify any bottlenecks or performance issues that may be occurring.
This command displays information about your computer’s CPU and I/O utilization.
How to Check CPU Utilization Via GUI?
You can also check the CPU utilization information via GUI as well, and for that, you can type the following command:
When you run this command in Ubuntu 22.04 then, a new window will appear, providing you with the relevant information as shown:
If this application is not installed in your Linux distribution, then you can do that by typing:
$ sudo apt install gnome-system-monitor # For Ubuntu $ sudo dnf install gnome-system-monitor # For Fedora $ sudo yum install gnome-system-monitor # For CentOS
You can also search for the ‘system Monitor’ application via application manager and this will also give you the same result:
Conclusion
Checking CPU utilization on a Linux system is a useful and important task for system administrators. It can be done by using some command line tools, i.e., top, htop, and ps. Moreover, a GUI application named “System Monitor” can also be installed and used to get CPU utilization. All these command line tools and the GUI method are briefly explained in this post.
How to Check CPU Utilization in Linux with Command Line
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.
- 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
Note: Use one of 5 available commands in Linux to check memory usage.
How To Check CPU Usage from Linux Command Line
top Command to View Linux CPU Load
Open a terminal window and enter the following:
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:
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:
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:
Once the process finishes, you can use the mpstat command in the terminal as follows:
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:
This would show you a report for the first processor (CPU 0).
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:
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:
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:
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:
To install to CentOS, enter the following:
The command to launch nmon is:
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:
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.
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.