Linux Commands

Find Processes Using Most CPU Linux

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.

 $ top

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.

$ top -i

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.

$ ps

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.

$ htop

Sar Command

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

 $ sar

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:

$ sar -u 2 5

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 &

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 &

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.

$ sar -f data.file

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.

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

$ iostat

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

$ iostat -xtc 5 3

vmstat command

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

$ vmstat

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

Enter the following information to install CentOS:

$ sudo yum epel-release
$ sudo yum install nmon

Launching nmon requires the following command:

$ nmon

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

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

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

$ yum install sysstat

dnf command for Fedora users

$ dnf install sysstat

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.

$ mpstat

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.

$ gnome-system-monitor

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

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

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.