How can I determine the current CPU utilization from the shell?
Categories:
Mastering CPU Utilization Monitoring from the Linux Shell

Learn various command-line tools and techniques to accurately measure and interpret CPU usage on Linux systems, from real-time monitoring to historical analysis.
Understanding CPU utilization is crucial for system administrators, developers, and anyone managing a Linux server. High CPU usage can indicate performance bottlenecks, misconfigured applications, or even malicious activity. This article explores several powerful command-line tools available in Linux that allow you to monitor CPU utilization effectively, providing both real-time insights and aggregate statistics.
Real-time CPU Monitoring with top
and htop
For immediate insights into system performance, top
and htop
are indispensable tools. They provide a dynamic, real-time view of running processes, CPU usage, memory consumption, and other vital system metrics. While top
is universally available, htop
offers a more user-friendly interface with color-coding, vertical and horizontal scrolling, and easier process management.
top
Running the top
command for real-time system monitoring.
htop
Running the htop
command (install if not present: sudo apt install htop
or sudo yum install htop
).
top
, press 1
to toggle the display of individual CPU cores. Press q
to quit. In htop
, use F1
for help, F6
to sort processes, and F10
to quit.Snapshot CPU Utilization with mpstat
and sar
While top
and htop
are great for real-time monitoring, tools like mpstat
and sar
(System Activity Reporter) are better suited for capturing CPU utilization snapshots or collecting historical data. These tools are part of the sysstat
package and provide detailed statistics on CPU usage per core, including user, system, idle, and I/O wait times.
mpstat -P ALL 1 5
Displays CPU utilization for all processors every 1 second, 5 times. -P ALL
shows per-processor statistics.
sar -u 1 5
Reports CPU utilization every 1 second, 5 times. The -u
option specifically requests CPU usage.
flowchart TD A[Start Monitoring] --> B{Choose Tool}; B -- Real-time --> C[top / htop]; B -- Snapshot/Historical --> D[mpstat / sar]; C --> E[Observe Processes & CPU%]; D --> F[Analyze Per-Core & Aggregate Stats]; E --> G[Identify Bottlenecks]; F --> G; G --> H[Optimize System]; H --> I[End Monitoring];
Decision flow for choosing CPU monitoring tools.
Understanding CPU Metrics
When analyzing CPU utilization, it's important to understand what the different metrics represent:
- %user: Percentage of CPU time spent executing user-level applications.
- %system: Percentage of CPU time spent executing kernel-level tasks.
- %idle: Percentage of CPU time when the CPU was idle and had no outstanding disk I/O requests.
- %iowait: Percentage of CPU time spent waiting for I/O operations to complete. High
iowait
can indicate disk bottlenecks. - %steal: (Virtualization environments) Percentage of time a virtual CPU waits for a real CPU while the hypervisor is servicing another virtual processor.
Monitoring these metrics helps pinpoint the root cause of high CPU usage, whether it's an application consuming too many resources, a kernel process, or I/O-bound operations.
sysstat
package (which includes sar
and mpstat
) often needs to be installed separately. On Debian/Ubuntu, use sudo apt install sysstat
. On RHEL/CentOS, use sudo yum install sysstat
.