How to check CPU usage in linux?

In this following post, you will learn how to check CPU usage in linux systems. Before that, to know how many CPUs are present in the system, run below command.

lscpu

In Linux, you can check CPU usage of the process using various commands. Here are a few commonly used commands:

1.top

The “top” command provides a real-time, interactive view of system processes. It displays information about CPU usage, memory usage, and more. By default, “top” command will work in most of linux distros, to verify if the package for top command is installed, execute below command.

rpm -qa | grep -i procps

From the above snap, you can see that the package for top command is installed and top command will work on your system, to verify, open a terminal and type:

top

Press “q” to exit the “top” command.

If the package is not installed, install the package using below command.

yum install procps-ng

Click Here to learn more about top command and it’s outputs.

2. htop

htop” is an enhanced version of “top” with a more user-friendly interface. To use “htop”, you need to install it first (if not already installed):

sudo apt-get install htop # For Debian/Ubuntu
sudo yum install htop # For CentOS/Fedora

Once installed, run:

htop

Use the arrow keys to navigate, and press “q” to exit.

3. mpstat

The “mpstat” command provides detailed information about CPU usage. To install it, run:

sudo apt-get install sysstat # For Debian/Ubuntu
sudo yum install sysstat # For CentOS/Fedora
After installation, you can use the following command to check CPU usage:
mpstat

4. sar

The “sar” command (System Activity Reporter) can also provide information on CPU usage. Install it with:

sudo apt-get install sysstat # For Debian/Ubuntu
sudo yum install sysstat # For CentOS/Fedora

Use the following command to check CPU usage:

sar

You can specify the interval and count with the “-u” option, for example:

sar -u 1 5 # Displays CPU usage every 1 second, 5 times

These commands should give you a good overview of CPU usage on your Linux system. Choose the one that suits your needs and preferences.

How do you feel about this post? Drop your comments below..