Command To Monitor Cpu Usage In Linux

6 min read

Introduction

Monitoring CPU usage in Linux is essential for system administrators, developers, and power users who want to keep their servers and workstations running smoothly. High CPU consumption can indicate performance bottlenecks, rogue processes, or resource‑intensive applications that need attention. By learning the most effective commands to monitor CPU usage in Linux, you can quickly identify the culprits, optimize workloads, and see to it that critical services remain responsive. This guide walks you through the primary tools, explains the underlying concepts, and answers common questions to help you master CPU monitoring on any Linux distribution Worth knowing..

Steps to Monitor CPU Usage

1. Use top – The Classic Real‑Time Overview

top is one of the oldest and most reliable utilities for viewing live system statistics. It displays processes sorted by CPU usage, allowing you to see which tasks are consuming the most resources at a glance.

top
  • Press P to toggle between absolute CPU percentages and cumulative CPU usage.
  • Press k to safely kill a process (requires root privileges).
  • Press q to exit.

top provides a quick snapshot but lacks detailed per‑core breakdowns and historical trends.

2. Install and Run htop – An Enhanced Alternative

htop is a colorful, more user‑friendly replacement for top. It shows per‑core usage, allows scrolling through the process list, and offers intuitive key bindings for killing processes.

sudo apt-get install htop   # Debian/Ubuntu
sudo yum install htop       # RHEL/CentOS
sudo dnf install htop       # Fedora
htop

Key features:

  • Per‑core visualization – each CPU core’s usage is displayed separately.
  • Tree mode – view processes and their child threads hierarchically.
  • CPU history graph – a graphical representation of overall CPU utilization over time.

3. put to work ps for Targeted Reporting

ps lets you extract CPU usage information for specific processes or groups. The aux format is especially useful because it shows all processes with detailed metrics.

ps aux --sort=-%cpu | head -n 10
  • --sort=-%cpu sorts by descending CPU usage.
  • head -n 10 limits output to the top ten consumers.

You can also filter by user, group, or command name using options like -U, -G, or -C.

4. Examine System‑Level Statistics with iostat

iostat, part of the sysstat package, reports CPU and I/O statistics at specified intervals. It’s ideal for spotting CPU spikes that coincide with disk activity Small thing, real impact..

sudo apt-get install sysstat   # Debian/Ubuntu
# or
sudo yum install sysstat       # RHEL/CentOS

Run iostat with a refresh interval (in seconds) and a count:

iostat -xz 1 5   # -x for extended statistics, -z to omit zero‑usage devices

The output includes columns such as %us (user space), %sy (system time), %wa (I/O wait), and %id (idle). Understanding these percentages helps you pinpoint whether CPU load is due to application code, kernel operations, or waiting on I/O.

5. Long‑Term Monitoring Using sar

sar, also from sysstat, aggregates CPU usage over time and writes data to a binary file (/var/log/sysstat/sa<date>). This is invaluable for capacity planning and trend analysis But it adds up..

Enable the sysstat service (if not already running) and schedule regular data collection:

sudo systemctl enable sysstat
sudo systemctl start sysstat

Generate a report for the past hour:

sar -u -f /var/log/sysstat/sa 3600

The -u flag selects CPU utilization statistics. The resulting table shows average CPU usage percentages, allowing you to compare peak periods against baseline performance.

6. Real‑Time Core Usage with vmstat

vmstat provides a snapshot of virtual memory, processes, I/O, CPU, and system events. When used with a short interval, it becomes a lightweight real‑time monitor Small thing, real impact..

vmstat 1 5

The CPU line displays five columns: us (user), sy (system), id (idle), wa (I/O wait), and st (steal time). For multi‑core systems, you can also check per‑core utilization via /proc/stat or tools like mpstat (part of sysstat) And that's really what it comes down to..

mpstat -P ALL 1 3

This command prints CPU statistics for each processor, helping you identify imbalanced load across cores.

7. Graphical Insight with ** glances**

glances is a modern, curses‑based monitoring tool that aggregates data from many sources, including CPU, memory, disk I/O, and network. It provides a single, colorful dashboard that updates in real time Simple as that..

pip install glances
glances

You can customize the layout, add or remove metrics, and even view historical trends with its built‑in graphing engine. glances is especially useful for quick visual checks and for users who prefer an all‑in‑one interface.

Scientific Explanation

CPU Utilization Metrics

When you monitor CPU usage in Linux, several percentage fields appear repeatedly:

  • User (us) – Time spent executing user‑space processes.
  • System (sy) – Time spent in kernel mode on behalf of processes.
  • Idle (id) – Percentage of time the CPU is idle.
  • I/O Wait (wa) – Time the CPU is waiting for I/O operations to complete.
  • Steal (st) – In virtualized environments, time the hypervisor steals from the guest.

These values are derived from the ticks the CPU spends in each state, measured by the kernel’s cpu_times structure. The sum of all non‑idle percentages indicates how much of the CPU’s capacity is actually being used for work No workaround needed..

How Commands Collect Data

  • top/htop read /proc/stat and /proc/[pid]/stat to calculate per‑process CPU time deltas over a short interval.
  • ps parses the same /proc/[pid]/stat files but presents a static snapshot unless combined with -o options for dynamic updates.
  • iostat and sar rely on kernel counters exposed via /proc/stat and /proc/diskstats, sampling them at defined intervals.
  • vmstat and mpstat also query /proc/stat and /proc/loadavg, aggregating the data into a concise table.

Each tool has its own strengths: top and htop

Each tool has its own strengths: top and htop excel at providing a dynamic, process-level view, making them ideal for identifying runaway applications. vmstat and mpstat offer system-wide statistics at a glance, perfect for spotting overall bottlenecks. ps is invaluable for quick snapshots and scripting, while glances delivers a comprehensive, real-time dashboard that aggregates multiple metrics into a single, colorful interface.

Quick note before moving on Worth keeping that in mind..

Choosing the Right Tool

For interactive monitoring, htop remains a favorite due to its user-friendly design and ability to kill processes directly. Here's the thing — in virtualized environments, paying attention to st in vmstat or mpstat can reveal hypervisor contention. But when diagnosing I/O-related stalls, vmstat's wa column and iostat's disk utilization are indispensable. For automated alerts and historical analysis, sar's data collection capabilities are unmatched. If you prefer a minimalist approach, combining ps with grep or awk can yield powerful insights without the overhead of a full TUI That alone is useful..

Conclusion

Effective CPU monitoring in Linux hinges on understanding both the metrics—user, system, idle, I/O wait, and steal time—and the tools that surface them. Also, whether you need a real-time dashboard like glances, a process-oriented view from htop, or system-wide statistics from vmstat and mpstat, each tool serves a distinct purpose. By matching the monitoring method to the diagnostic goal, administrators can quickly identify performance issues, balance workloads across cores, and make sure computational resources are fully utilized without unnecessary contention. The key is to practice with these tools regularly, so that interpreting their output becomes second nature, enabling swift and informed troubleshooting in any Linux environment Worth keeping that in mind. Took long enough..

Right Off the Press

Fresh Off the Press

Handpicked

In the Same Vein

Thank you for reading about Command To Monitor Cpu Usage In Linux. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home