Many times such situations come in Linux where we have to deal with application unresponsiveness or sluggish applications due to CPU usage. This happens when our Linux system CPU is very busy. Most of the time, it has to wait until the CPU is free and queue up to process its pending requests.

Whenever the CPU is completely occupied by the processes of the Linux system, it becomes difficult for the CPU to process other requests. All remaining requests have to be stopped until the CPU is freed. This becomes a huge obstacle.

We have to use the appropriate command to know the processes which are affecting the CPU. In this article, we will tell how we can solve CPU-related problems with the help of the following commands, as well as see how to find those processes which are using most CPU Linux.

Find Processes Using Most CPU Linux

Finding CPU usage is a very important task. Linux comes with several utilities to report CPU usage.

  1. CPU usage
  2. Provide an overview of each CPU’s utilization (SMP CPUs)
  3. How much CPU has been used since your last reboot
  4. Determine what’s consuming the CPU (or CPUs)

We have many such ways with the help of which we can see the use of CPU in Linux processes.

Top command (Table of Process or TOP)

Everyone uses the top command to monitor Linux systems because it is good and well-known. By top command, the system provides a dynamic overview of the cpu usage. Top command frequently produces an updated list of top command processes. This command shows us how the CPU is using memory and processing power as well as gives information about other running processes.

The top command displays a list of tasks currently managed by the Linux kernel, as well as a complete system summary. This command monitors memory usage, process statistics and CPU usage.

Its top section contains a lot of useful information related to the overall system status, including usage statistics for both swap space and memory, CPU status, process count, load average, uptime, etc.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-1.png" data-lazy- height="476" src="data:image/svg xml,” width=”719″>

This list may change frequently but you can press the Q button to stop the refresh process. We can start apex using the -i switch.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-2.png" data-lazy- height="333" src="data:image/svg xml,” width=”716″>

It hides all inactive processes to make it easier to sort the list. Use the following commands:

  • M: Sort the task list as per the memory used by the system
  • P: Sort tasks based on CPU usage
  • N: Sort tasks according to processes
  • T: Sort task list by run time

By default, this command sorts the top output with CPU usage by updating the top command data every 5 seconds.

For the purpose of further analysis, it is best to have a clear view of top’s output after running it in batch mode. Using the top command, you can find out what the CPU statistics are for Linux.

Top command performs well on multiple CPUs and in true SMP environments, the top number of CPUs. To exit from the top command display, we have to type the q key.

ps Command (Process Status)

PS command displays information about the active running processes on the system. This command provides a snapshot of current processes with detailed information like time command name, process start date, memory usage, CPU usage, user id, username etc.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-3.png" data-lazy- height="77" src="data:image/svg xml,” width=”728″>

This command provides a snapshot of current processes with detailed information like time command name, process start date, memory usage, CPU usage, user id, username etc.

htop Command

The htop command is also similar to the top command, but one of its features is that it allows us to scroll horizontally and vertically and much more.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-4.png" data-lazy- height="571" src="data:image/svg xml,” width=”717″>

Sar Command

With sar command, you can view today’s CPU activity as a utility for managing system resources.

The -u option allows you to track CPU performance, but it is not limited exclusively to CPU usage.

You can print the standard output by the sar command on selected cumulative activity counters in the OS. It gives output based on interval and calculation parameters. For example, 2 seconds apart; 5 times, use:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-5.png" data-lazy- height="295" src="data:image/svg xml,” width=”726″>

Display CPU usage with the –u option. Whenever it displays, it should do so every five seconds. You can cancel this by pressing Ctrl-C.

Here, U12 5 displays the following values of CPU usage.

  • % user: Displaying CPU usage while executing at the application and user level.
  • %nice: Showing CPU usage for the user while an application is running.
  • %system: Displaying CPU usage while executing at the (kernel) system level.
  • %iowait: This measure tracks the amount of idle time over which the system has outstanding disk I/O requests.
  • %idle: It shows the percentage of time when CPUs or CPUs are idle and during which the system did not have outstanding I/O disk requests.

The output file has to be set for multiple reports and multiple samples to get the report from the Sar command. Run this command as a background process.

$ sar -o output.file 12 8 >/dev/null 2>&1 &

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-6.png" data-lazy- height="38" src="data:image/svg xml,” width=”717″>

Even after logging out, if we want to see the report back, then we can better use the nohup command.

$ nohup sar -o output.file 12 8 >/dev/null 2>&1 &

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-7.png" data-lazy- height="43" src="data:image/svg xml,” width=”697″>

This file contains binary copies of all the data contained in the file. Using the -f option, you can display this data with the sar command.

Find out who is consuming CPUs or monopolizing them. In the end, we determine which process is consuming and monopolizing the CPU. The following commands display the top 10 CPU users on Linux systems.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/find-processes-patch-1.png" data-lazy- height="221" src="data:image/svg xml,” width=”720″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-8.png" data-lazy- height="294" src="data:image/svg xml,” width=”717″>

iostat Command

This command is used to find the average usage of the system since the last reboot. By using the iostat command, we report the partitions, devices and data of the CPU (Central Processing Unit).

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-9.png" data-lazy- height="364" src="data:image/svg xml,” width=”721″>

If we use the command given below, we will get 3 outputs every 5 seconds.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-10.png" data-lazy- height="391" src="data:image/svg xml,” width=”723″>

vmstat command

With vmstat, you can check CPU, disk, IO, memory, traps, processes, and more. This command goes something like this.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-11.png" data-lazy- height="139" src="data:image/svg xml,” width=”727″>

The percentages represent the time spent on each CPU core.

  • us: Runtime for non-kernel code.
  • sy: Duration of kernel execution.
  • id: The number of idle minutes.
  • wa: Waiting time for I/O.
  • st: Time stolen from a virtual machine.

nmon Command

Nigel Griffiths of IBM developed the monitoring tool Nmon. Follow these instructions to install the monitoring tool Nmon:

$ sudo apt-get install nmon

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-12.png" data-lazy- height="141" src="data:image/svg xml,” width=”721″>

Enter the following information to install CentOS:

$ sudo yum epel-release


$ sudo yum install nmon

Launching nmon requires the following command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-13.png" data-lazy- height="45" src="data:image/svg xml,” width=”704″>

There are different keys for different options as you can see in the image given below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-14.png" data-lazy- height="516" src="data:image/svg xml,” width=”707″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-15.png" data-lazy- height="194" src="data:image/svg xml,” width=”727″>

You can press the Q button to exit the tool.

mpstat and Other Tools

To utilize these features we must first install a systat package. Default tools and system performance tools such as RHEL / Red Hat Linux are included in this package for Linux. We can install it on Ubuntu or Debian by the following command:

$ sudo apt-get install sysstat

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-16.png" data-lazy- height="75" src="data:image/svg xml,” width=”650″>

To install sysstat for different versions of Linux, we have to use the command according to them.

up2date command for CentOS/RHEL Linux v4.x or older

$ up2date install sysstat

yum command for Oracle/RHEL/CentOS Linux v5.x or newer

dnf command for Fedora users

By using mpstat, display the use of each CPU individually. If we use multiple CPU and SMP systems, then we use mpstat command to display the usage of different CPUs. This command provides us with the report of statistics related to the processor.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-17.png" data-lazy- height="131" src="data:image/svg xml,” width=”723″>

Process 0 is the first available processor, and mpstat displays activity for each one. The mpstat command is used on both UP and SMP machines, but the latter will print only the global average of activities.

  • %usr: CPU utilization at the user level
  • %nice: CPU usage for “nice” processes
  • %sys: CPU usage on the kernel (system-level)
  • %iowait: CPU idle while disk read/write occurs
  • %irq: CPU usage during hardware interruptions
  • %soft: Handling software interrupts with CPU usage
  • %steal: CPU usage forced to wait for another virtual processor to finish handling
  • %guest: Processor usage while a virtual processor is running
  • %idle: CPU usage during idle time (no processes running, and no disk read/write waiting)

GUI tools to Find Process Using Most CPU in Linux

The above tool/command is very useful for remote servers. We can try gnome-system-monitor with X GUI for local systems. It lets us monitor and control the system’s processes. Through this, we can terminate processes, send signals, and access memory maps.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-18.png" data-lazy- height="77" src="data:image/svg xml,” width=”714″>

Additionally, gnome-system-monitor shows us an overall view of resource usage on our system including CPU and memory allocation.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Find-Processes-Using-Most-CPU-Linux-19.png" data-lazy- height="455" src="data:image/svg xml,” width=”728″>

Conclusion

In this article we have given information about many Linux command line tools. Through all these commands, we can see how we can find out the maximum usage of CPU and stop it. Through this, we can stop its usage and move forward the second processing of CPUs.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/a6dca3a5394c9d066a8078ddf9ede366?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.