Linux Commands to Monitor CPU Usage: A full breakdown
Monitoring CPU usage is essential for system administrators, developers, and any power user aiming to maintain optimal performance, troubleshoot bottlenecks, or simply keep an eye on system health. Linux offers a rich set of command-line tools that provide real-time and historical insights into CPU activity. This guide explores the most effective commands—ranging from simple one-liners to advanced diagnostic utilities—ensuring you have the knowledge to choose the right tool for your needs Easy to understand, harder to ignore..
1. top: The Real-Time System Monitor
The top command is the go-to tool for immediate, interactive CPU monitoring. Upon execution, it displays a dynamic, color-coded list of processes sorted by CPU consumption, alongside overall system statistics That's the part that actually makes a difference. Less friction, more output..
Key Features:
- Real-Time Updates: Refreshes the display every 3 seconds by default (adjustable with
-d). - Process Sorting: By default, processes are sorted by CPU usage. Press
Shift + Pto toggle sorting by CPU, memory, or other metrics. - Interactive Controls: Use
kto kill a process,rto renice, orqto quit.
Interpreting Output: The top section shows aggregate CPU usage broken down into:
- User Time (
us): Time spent in user space (e.g., applications). - System Time (
sy): Time in kernel space (e.g., system calls). - Nice Time (
ni): User processes with adjusted priority. - Idle (
id): CPU downtime (higher is better). - I/O Wait (
wa): Time waiting for disk I/O (indicates potential storage bottlenecks). - Hardware Interrupts (
hi/si): Time handling hardware/software interrupts.
Example Usage:
top -d 1 # Refresh every 1 second
top -u username # Filter by user
2. htop: An Enhanced Alternative to top
htop is an interactive process viewer with a more user-friendly interface, often described as top on "graphical steroids.Day to day, g. Which means " It requires installation via package managers (e. , sudo apt install htop).
Advantages Over top:
- Color-Coded Output: Easier to distinguish process states.
- Tree View: Visualize process hierarchy with
F5. - Mouse Support: Click to select processes or change settings.
- Simpler Process Management: Use
F9to kill a process orF7to adjust priority.
Key Metrics:
- Displays CPU usage as a graph per core, making it easy to spot uneven load distribution.
- Shows memory and swap usage in a progress bar format.
Example Usage:
htop -u username # Filter by user
htop -p 1234 # Monitor specific PID
3. vmstat: System Activity Snapshot
vmstat (virtual memory statistics) provides a concise summary of CPU, memory, I/O, and system activity. It’s ideal for quick checks without interactive overhead.
Syntax and Output:
vmstat [delay] [count]
Example output (sampled every 2 seconds, 3 times):
procs -----------memory---------- --swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 0 123456 7890 45678 0 0 20 30 100 200 5 3 92 0
0 0 0 123400 7890 45700 0 0 0 40 110 210 8 2 90 0
CPU Columns Explained:
us: User time (non-nice processes).sy: System time.id: Idle time.wa: I/O wait (time spent waiting for disk).
4. mpstat: Per-CPU Statistics
Part of the sysstat package, mpstat breaks down CPU usage by individual cores or processors. This is crucial for identifying core-specific bottlenecks.
Example Output:
mpstat -P ALL 1 3
Output:
Average: cpu %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
Average: all 5.25 0.05 2.10 0.30 0.05 0.05 0.00 0.00 0.00 92.20
Average: 0 8.40 0.00 3.20 0.50 0.10 0.10 0.00 0.00 0.00 87.70
Average: 1 2.10 0.10 1.00 0.10 0.00 0.00 0.00 0.00 0.00 96.80
Here, all shows aggregate usage, while 0 and 1 represent individual cores. High %iowait on a specific core may indicate a disk-bound process pinned to that CPU It's one of those things that adds up. No workaround needed..
5. iostat: CPU and I/O Insights
Also from sysstat, iostat combines CPU statistics with disk I/O metrics, making it invaluable for diagnosing storage-related CPU delays.
Example:
iostat -x 1 3
Key CPU metrics include %iowait (already discussed) and %steal (time stolen by hypervisors in virtualized environments) Which is the point..
6. sar: Historical Activity Reporting
sar (System Activity Reporter) collects and stores CPU data periodically, allowing historical analysis. It’s perfect for identifying trends or performance issues after the fact.
Setup:
Ensure sysstat is installed and the cron job is enabled:
sudo systemctl enable sysstat
sudo systemctl start sysstat
Viewing Data:
sar -u # CPU usage for today
sar -u -s 10:00 # From 10:00 AM onward
Output includes %user, %system, %nice, %iowait, and %idle over time.
7. nproc and lscpu: Hardware Inventory
Before diving into usage, understand your CPU topology:
nproc: Prints the number of processing units (cores/threads).lscpu: Detailed CPU architecture information (model, cores, threads, caches).
Example:
lscpu