How to Check CPU Utilization in Linux with Command Line?


Introduction

Monitoring the performance of a Linux system is essential to ensuring that it is operating optimally. One of the key factors in determining the performance of a system is CPU utilization.

CPU utilization refers to the percentage of time that the processor spends executing instructions from various processes and applications on the system. In Linux, there are numerous tools available for monitoring CPU utilization, but using command line tools provides a quick and efficient way to check this metric.

Basic Command Line Tools for Checking CPU Utilization

When it comes to monitoring CPU utilization with the command line in Linux, you have several options. Let's start with some basic tools that are available on most Linux distributions.

Top: Overview of the top Command and how to use it to Check CPU Utilization

The top command is a powerful tool for monitoring system processes, including CPU utilization. When you run top, you'll see a list of running processes sorted by default according to their CPU usage (as well as other metrics like memory usage).

You can press 'q' at any time to exit. To sort the processes by other criteria like memory usage or process ID, press 'F' and select the desired sorting option.

$ sudo top

Output

top - 12:34:56 up 1 day, 00:00:00, 1 user, load average: 0.50, 0.75, 0.80
Tasks: 123 total, 1 running, 122 sleeping, 0 stopped, 0 zombie
%Cpu(s): 10.5 us, 3.8 sy, 0.0 ni, 84.1 id, 1.6 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem :  3963.2 total,  1023.2 free,  2056.7 used,   883.3 buff/cache
MiB Swap:  4096.0 total,  4096.0 free,     0.0 used.  1523.4 avail Mem

Htop: Introduction to Htop, a More Advanced Version of top With Additional Features

If you're looking for something more advanced than top but still fairly straightforward to use, htop might be what you need. Htop is similar in functionality to top but comes with several additional features that make it even more useful.

For example, htop allows filtering of processes based on name or status and can display all threads associated with a specific process group. To use htop simply type "htop" into your terminal and hit enter.

$ sudo htop

Output

1  [|||||                      18.6%]   Tasks: 195, 16 thr; 1 running
2  [|||||                      17.2%]   Load average: 0.74 0.89 0.93 
3  [|||||                      16.9%]   Uptime: 2 days, 3 hours, 15 mins
4  [||||||                     21.8%] 
5  [|||||                      18.7%]   Mem[||||||||||||||| 1.67G/7.79G]
6  [||||                       12.4%]   Swp[|                      0K/0K]

  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
 1370 root       20   0  3.1G  437M  136M S 10.1  5.6 12:48.24 /usr/bin/compiz
 2398 john       20   0  1.8G  272M   76M S  8.3  3.5  5:21.53 /usr/bin/firefox

Glances: Overview of Glances, a Comprehensive System Monitoring Tool That Includes CPU Utilization Information

Glances is a more comprehensive system monitoring tool than top or htop. It provides real-time information about the system's performance, including CPU utilization.

Glances also gives you information about disk usage, network activity, and memory usage. To use glances simply type "glances" into your terminal and hit enter.

$ glances

Output

CPU:  [#####   ]  45.2%

Overall, these basic tools should provide you with enough functionality to monitor CPU utilization on most Linux systems. However, if you need more advanced features like process tracing or analysis, you may want to consider some of the more specialized tools we'll cover next.

Advanced Command Line Tools for Checking CPU Utilization

Perf: The Power Tool for Performance Analysis

Perf is a powerful performance analysis tool that can be used to monitor and analyze various aspects of system performance including CPU utilization. It provides a wide range of features for profiling applications, kernel tracing, and hardware event monitoring. Perf can help identify CPU usage bottlenecks, track down hardware issues, and optimize system performance.

To use perf, you need to have the perf tool installed on your Linux system. You can install it using the package manager of your distribution.

$ perf stat -e cpu-clock python my_script.py

Performance counter stats for 'python my_script.py':
   10,387,682,725      cpu-clock (msec)          # CPU clock cycles
      0.498089      seconds time elapsed

Sysstat: Keeping Track of System Activity

Sysstat is a collection of tools that includes sar (system activity reporter) which can be used to collect and report on system activity including CPU utilization over time. Sar collects various system statistics such as CPU usage, memory usage, disk I/O operations, network traffic, and more.

To use sar, you need to have the sysstat package installed on your Linux system. You can install it using the package manager of your distribution.

Once installed, sar runs automatically in the background and collects data at regular intervals. You can view sar reports using the 'sar' command followed by various options such as time range (-s start_time -e end_time) or output format (-o output_file).

$ sar -u 1 5

Output 

Linux 5.4.0-81-generic (hostname)    06/17/23    _x86_64_    (2 CPU)

07:36:19 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
07:36:20 AM     all      3.25      0.00      0.75      0.00      0.00     95.00
07:36:21 AM     all      2.50      0.00      0.50      0.00       0.00     97.00
07:36:22 AM     all      1.50      0.00      0.50      0.00       0.00     98.00
07:36:23 AM     all      2.50      0.00      0.00      0.00       0.00     97.50
07:36:24 AM     all      3.50      0.00      0.00      0.00       0.00     96.50
Average:        all      2.85      0.00      0.35      0.00       0.00     96.90

Niche Subtopics in Checking CPU Utilization

Using awk or Grep Commands With top or Htop Output to Filter and Extract Specific Information About CPU Usage

While the top and htop commands provide great insight into overall system CPU utilization, sometimes we may need to drill down further and extract more specific information. This is where the power of awk and grep come in. These commands can be used to filter top or htop output based on specific criteria, such as sorting by process name or displaying only active processes.

For example, if we want to display only the processes using more than a certain threshold of CPU usage, we can use awk with the following command −

$ top -b -n 1 | awk '{if ($9 > 10) print}' 

This command will display only the processes whose CPU usage is greater than 10%. We can also use grep command with Top to filter out specific process names.

Monitoring Individual Processes Using Pidstat or ps Commands

Often times, it's not enough to just monitor overall system CPU utilization. Sometimes we need to focus on individual processes that may be using excessive amounts of CPU resources.

This is where pidstat comes in handy. The pidstat command allows us to monitor individual process statistics including CPU utilization over time.

To monitor a specific process, simply run the following command −

$ pidstat -p [process id] [interval] [count]  

This will give us a detailed report on that particular process's resource consumption at a given interval for a specified number of counts. Alternatively, we can use ps command along with its aux option which provides detailed information about all running processes including their PID and other relevant details like memory/CPU usage etc.

$ ps aux  

By using these niche subtopics in combination with basic tools like top and htop, we can gain a much deeper understanding of our system's CPU utilization and take actions to optimize it for better performance.

Conclusion

Checking CPU utilization in Linux using command line tools can provide valuable information about system performance and help diagnose issues with processes or groups. While basic tools like top and htop provide adequate information for most users; advanced tools like perf or sysstat can give more detailed insights into performance metrics.

By understanding small details like user, system, and idle times combined with using proper command-line tools to check CPU utilization; sysadmins or users can keep their systems running optimally and make informed decisions when troubleshooting performance issues. Overall, monitoring your CPU utilization regularly will ensure you're making optimal use of your resources while keeping your programs running as smoothly as possible!

Updated on: 31-Aug-2023

453 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements