How to Check CPU Utilization in Linux?

Learn via video courses
Topics Covered

Monitoring CPU utilization is a crucial task for system administrators, developers, and users who want to ensure the efficient performance of their Linux systems. By monitoring CPU usage, you can identify bottlenecks, detect resource-intensive processes, and optimize system performance. Linux provides several powerful tools and commands that allow you to measure and analyze CPU utilization effectively. In this guide, we will explore different methods to check CPU utilization in Linux.

One of the most commonly used commands for monitoring CPU utilization is "top". The "top" command provides real-time information about system resource usage, including CPU usage. It displays a dynamic list of processes sorted by resource consumption, with the CPU utilization prominently displayed. We will delve into the different features and options of the "top" command to extract valuable insights about CPU usage.

Causes of High CPU Utilization

High CPU utilization in Linux can be attributed to various factors and underlying causes. Understanding these causes is essential for troubleshooting and optimizing system performance. Here are some common reasons for high CPU utilization in Linux:

  • Intensive Processes:
    CPU-intensive processes, such as complex calculations, data manipulation, or resource-intensive applications, can consume a significant portion of the CPU's processing power. This can result in high CPU utilization, especially if multiple resource-intensive processes are running simultaneously.
  • Insufficient System Resources:
    If the system lacks sufficient resources (e.g., CPU cores, memory, disk I/O), the available CPU may struggle to handle the workload efficiently. Insufficient resources can lead to increased CPU utilization as the system tries to cope with the demands placed on it.
  • Background Processes and Services:
    Background processes and services running on the system can consume CPU resources, even if they are not directly associated with user-initiated activities. Examples include system daemons, scheduled tasks, or automatic updates. If these processes become poorly optimized or misconfigured, they can cause high CPU usage.
  • Malware or Security Threats:
    Malicious software, such as viruses, worms, or botnets, can infiltrate a Linux system and run unauthorized processes in the background. These processes may consume significant CPU resources, resulting in high CPU utilization. Regular security scans and monitoring are crucial to detect and mitigate such threats.
  • Inefficient or Misbehaving Applications:
    Poorly coded or misbehaving applications can consume excessive CPU resources due to bugs, memory leaks, inefficient algorithms, or improper resource management. These applications can monopolize CPU time and lead to high CPU utilization, affecting the overall system performance.

Various Ways to Check CPU Utilization

Using "top" Command

One of the most commonly used commands to check CPU utilization in Linux is the "top" command. It provides real-time information about system resource usage, including CPU usage. Here's how you can use the "top" command to check CPU utilization:

  1. Open the terminal on your Linux system.

  2. Type the following command:

  3. Once executed, the "top" command will display a dynamic list of processes, with the CPU utilization information at the top of the screen.

    • The "%CPU" column represents the percentage of CPU usage for each process.
    • The "%CPU(s)" line at the top displays the overall CPU usage for all processes combined.
  4. By default, the processes are sorted in descending order of CPU usage. The process at the top of the list is the one consuming the most CPU resources.

  5. To exit the "top" command, you can press the "q" key.

  6. Additionally, the "top" command provides several interactive features:

    • Pressing the "k" key allows you to send a signal (e.g., terminate or kill) to a selected process.
    • Pressing the "1" key (numeric) shows individual CPU core utilization if your system has multiple cores.
    • Pressing the "f" key allows you to choose which columns to display and customize the "top" output.

Using "htop" Command

One of the popular and user-friendly commands to check CPU utilization in Linux is "htop". Htop provides an interactive and visually appealing interface to monitor system resources, including CPU usage. Here's how you can use the htop command to check CPU utilization:

  1. Open a terminal on your Linux system.

  2. Install htop if it's not already installed. You can typically install it using package managers like apt or yum. For example, on Ubuntu or Debian-based systems, you can run the following command to install htop:

  3. Once htop is installed, simply run the command:

  4. The htop interface will open, displaying real-time information about system resources. By default, the CPU utilization is shown in the header section.

  5. The CPU usage is represented graphically, displaying a bar for each CPU core. The length of the bar represents the utilization percentage of the respective CPU core.

  6. You can also view the detailed CPU usage for each process running on the system. The processes are listed in a table below the CPU utilization graph, sorted by their resource consumption. The "%CPU" column indicates the percentage of CPU usage for each process.

  7. Use the arrow keys to navigate through the processes and the F keys to access additional options and filters. For example, pressing F5 will sort the processes based on different criteria like CPU usage or memory usage.

  8. To exit the htop, simply press the "q" key.

Using "mpstat" Command

To check CPU utilization using the "mpstat" command, follow these steps:

  1. Open a terminal on your Linux system.

  2. Type the following command and press Enter to display CPU utilization statistics:

    By default, the "mpstat" command provides an overall summary of CPU usage, including average values since the system was booted.

  3. To get more detailed information, you can specify the interval at which the command should provide updates. For example, to display CPU utilization every 2 seconds, use the following command:

    The output will continuously update with the specified interval, providing real-time CPU utilization data.

  4. By default, "mpstat" displays average CPU utilization across all cores. If you want to view individual CPU core statistics, you can specify the "-P ALL" option. For example:

    This command will show CPU utilization for each core separately, allowing you to identify any imbalances or specific cores with high usage.

  5. You can also use the "-I ALL" option to display interrupt statistics related to CPU usage. This can provide insights into interrupt-driven activity that may contribute to CPU utilization. For example:

    The output will include information about interrupts, context switches, and other related statistics.

Using "iostat" Command

To check CPU utilization using the iostat command in Linux, follow these steps:

  1. Open a terminal or SSH into your Linux system.

  2. Run the following command:

    This will display the CPU utilization statistics for all CPUs on your system.

    If the iostat command is not available on your system, you may need to install it. On Debian-based systems, you can use the following command to install it:

    On Red Hat-based systems, you can use the following command:

  3. The output of the iostat command will include various columns of information. The column that represents CPU utilization is "%idle". The "%idle" column indicates the percentage of time the CPU is idle, while the remaining percentage represents CPU usage.

    • If "%idle" is high (close to 100%), it means the CPU is mostly idle.
    • If "%idle" is low (close to 0%), it means the CPU is heavily utilized.
  4. You can customize the output of the iostat command by specifying additional options. For example, to display CPU utilization statistics at regular intervals, you can use the following command:

    This will continuously refresh the CPU utilization statistics every second.

  5. Additionally, you can specify the number of iterations to display before exiting the command. For example, to display the CPU utilization statistics for five iterations, with a one-second interval between each iteration, you can use the following command:

Using the "vmstat" Command

One of the ways to check CPU utilization in Linux is by using the "vmstat" command. "vmstat" is a versatile command-line tool that provides detailed information about system memory, CPU usage, and other vital statistics. To check CPU utilization using "vmstat," follow these steps:

  1. Open a terminal or SSH into your Linux system.

  2. Run the following command:

The "1" parameter specifies the update interval, indicating that "vmstat" should refresh the output every second.

  1. Observe the output displayed by "vmstat." Look for the "CPU" section, which provides information about CPU utilization.

  2. Monitor the "us," "sy," and "id" columns to gauge CPU utilization. Higher values in the "us" and "sy" columns indicate increased CPU usage, while a higher value in the "id" column suggests more idle CPU time.

It's important to note that "vmstat" provides real-time statistics, allowing you to monitor CPU utilization dynamically. You can stop the command by pressing Ctrl+C.

::

Other Ways to Monitor CPU Performance

Using "ps" Command

The "ps" command is a versatile tool in Linux that provides information about active processes. It can also be used to check CPU performance and monitor CPU utilization. Here's how you can use the "ps" command to monitor CPU performance:

  1. Open a terminal or SSH into your Linux system.

  2. Run the following command to display a list of running processes along with their CPU usage:

    This command uses the "-eo" option to specify the output format, including the process ID (pid), parent process ID (ppid), CPU usage (%cpu), memory usage (%mem), and command (cmd).

  3. The output will show the processes running on your system and their respective CPU utilization percentages.

    • The "%cpu" column indicates the percentage of CPU time used by each process.
    • The higher the "%cpu" value, the more CPU resources the process is consuming.
  4. Monitor the "%cpu" column to observe the CPU usage of different processes in real-time.

    • Processes with consistently high CPU percentages may indicate resource-intensive or misbehaving applications.
    • Note that the "%cpu" value represents the CPU usage since the process started, not necessarily the current instantaneous usage.
  5. You can sort the output based on CPU usage to identify the most CPU-intensive processes. Run the following command to sort the output by descending CPU usage:

    The "--sort=-%cpu" option sorts the output based on the "%cpu" column in descending order.

  6. Use the "top" command in conjunction with "ps" for real-time monitoring.

    Open a terminal and run:

    The "top" command provides an interactive interface that continuously updates information about system processes, including CPU utilization. It displays the most CPU-intensive processes at the top of the list.

    • Press "q" to exit the "top" command when you're done monitoring.

Using "nmon" Command

The "nmon" command is a powerful and widely used tool for monitoring system performance, including CPU utilization, on Linux systems. It provides a comprehensive set of features and displays real-time data in a detailed and customizable format. To monitor CPU performance using the nmon command, follow these steps:

  1. Install nmon (if not already installed):

    • On Debian/Ubuntu-based systems:

    • On CentOS/RHEL-based systems:

    • On Fedora systems:

  2. Open a terminal or SSH into your Linux system.

  3. Run the following command to launch nmon:

  4. The nmon interface will be displayed, showing various performance metrics.

  5. To focus on CPU utilization, press the "c" key. This will display detailed CPU-related information.

    • The nmon CPU screen provides real-time statistics on CPU usage, including percentages for user, system, and idle time.
    • It also shows additional metrics such as CPU frequency, interrupt rates, context switches, and more.
  6. Monitor the CPU utilization metrics provided by nmon.

    • The user percentage indicates the CPU time spent on user processes.
    • The system percentage represents the CPU time spent on system-level processes.
    • The idle percentage indicates the amount of idle CPU time.
  7. Use the arrow keys to navigate through different screens in nmon, such as CPU core utilization, CPU cache usage, and more.

  8. To exit nmon, press the "q" key.

Using Graphical Utility Option

Apart from command-line tools like Glances, Linux also offers graphical utilities that provide an intuitive and visual representation of CPU performance. These tools present CPU utilization data in graphs, making it easier to analyze and monitor the system. Here are a few popular graphical utilities for monitoring CPU performance in Linux:

  1. GNOME System Monitor:
    GNOME System Monitor is a feature-rich graphical utility that provides real-time monitoring of system resources, including CPU utilization.

  2. KDE System Monitor (KSysGuard):
    KDE System Monitor, also known as KSysGuard, is the equivalent utility for systems running the KDE Plasma desktop environment. It offers similar functionality to GNOME System Monitor, providing a graphical representation of CPU usage. You can typically find KSysGuard in the system menus or by searching for "System Monitor" or "KSysGuard".

  3. Conky:
    Conky is a highly customizable system monitor that can be configured to display various system information, including CPU utilization, on the desktop.

  4. Xfce Task Manager:
    Xfce Task Manager is the default task manager for the Xfce desktop environment. It provides a straightforward graphical interface to monitor system resources, including CPU usage.

Learn More

To learn more about some other essential commands in Linux you can check out the following articles-

Conclusion

  • Linux provides various command-line tools for monitoring CPU utilization, such as "top," "htop," "sar," and "mpstat".
  • Graphical utilities like GNOME System Monitor, KDE System Monitor (KSysGuard), Conky, and Xfce Task Manager provide a visual representation of CPU utilization in graphs or percentage breakdowns of individual cores.
  • Learning the options and features of these tools allows you to customize their display and analyze CPU performance more effectively.