How to Check CPU Utilization in Linux with Command Line?

Monitoring the performance of a Linux system is essential to ensuring that it operates optimally. One of the key factors in determining system performance 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 Command

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, along with other metrics like memory usage.

$ top
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

The CPU line shows important metrics: us (user time), sy (system time), ni (nice time), id (idle time), wa (I/O wait), and others. You can press 'q' to exit or 'F' to sort by different criteria.

Htop Command

Htop is an enhanced version of top with additional features and a more user-friendly interface. It provides visual CPU usage bars, allows filtering of processes, and displays threads associated with process groups.

$ htop
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 Command

Glances is a comprehensive system monitoring tool that provides real-time information about CPU utilization, memory usage, disk activity, and network statistics in a single view.

$ glances
CPU:  [#####   ]  45.2%
MEM:  [###     ]  32.1%
SWAP: [        ]   0.0%

Advanced Command Line Tools

Perf Performance Analysis Tool

Perf is a powerful performance analysis tool for monitoring CPU utilization, profiling applications, and tracking hardware events. It helps identify bottlenecks and optimize system performance.

$ 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

SAR System Activity Reporter

SAR (from the sysstat package) collects and reports system activity including CPU utilization over time. It runs automatically in the background and provides historical data.

$ sar -u 1 5
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

Advanced Filtering and Process Monitoring

Filtering with AWK and Grep

You can use awk and grep to filter top output for specific CPU usage thresholds or process names:

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

This displays only processes using more than 10% CPU.

Individual Process Monitoring

Use pidstat to monitor specific processes by PID:

$ pidstat -p [process_id] [interval] [count]

Or use ps for detailed information about all running processes:

$ ps aux

Command Comparison

Tool Best For Key Features
top Basic monitoring Real-time process list, simple interface
htop Enhanced monitoring Visual bars, process filtering, user-friendly
glances Comprehensive overview All-in-one system metrics display
perf Performance analysis Hardware counters, profiling
sar Historical data Time-series data, trend analysis

Conclusion

Linux provides numerous command line tools for checking CPU utilization, from basic utilities like top and htop to advanced tools like perf and sar. Understanding these tools and their capabilities enables system administrators to monitor performance effectively and troubleshoot issues. Regular CPU monitoring ensures optimal resource utilization and maintains system performance.

Updated on: 2026-03-17T09:01:39+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements