The most direct method to check CPU utilization in Linux involves a simple command line tool. If you’ve ever wondered how to check cpu utilization in linux command, the answer starts with a built-in utility called top. This guide walks you through every major command, from basic to advanced, so you can monitor performance like a pro.
Understanding CPU usage is critical for troubleshooting slow systems or optimizing server workloads. Linux offers multiple commands, each with its own strengths. You’ll learn the top tools, how to interpret their output, and when to use each one.
How To Check Cpu Utilization In Linux Command
The top command is the go-to for real-time CPU monitoring. It shows a dynamic view of running processes, CPU usage per core, memory stats, and more. To start, simply open your terminal and type:
top
Press q to exit. The top section displays overall CPU percentages: us for user processes, sy for system processes, id for idle time, and wa for I/O wait. This is your first answer to “how to check cpu utilization in linux command” in real time.
For a cleaner, one-shot view, use top -bn1. This runs a single batch iteration and exits, perfect for scripts or quick checks. The output includes the same CPU line but without the interactive interface.
Using Htop For A User-Friendly View
If top feels cluttered, install htop. It’s not pre-installed on all distros, so run:
sudo apt install htop # Debian/Ubuntu
sudo yum install htop # RHEL/CentOS
Then launch with htop. You’ll see color-coded bars for each CPU core, process trees, and memory usage. Navigate with arrow keys, and press F10 to quit. Htop is often easier to read for beginners.
One advantage of htop is that you can sort processes by CPU usage interactively. Just click on the CPU column header or press F6 and select PERCENT_CPU. This makes finding resource hogs straightforward.
Checking Cpu Utilization With The Mpstat Command
For per-core statistics, mpstat is your friend. It’s part of the sysstat package. Install it if missing:
sudo apt install sysstat
Then run:
mpstat -P ALL 1 3
This shows CPU usage for all cores, updating every second for three iterations. The output includes %usr, %sys, %idle, and %iowait. It’s ideal for spotting uneven load across cores.
To see a summary without per-core breakdown, just type mpstat. It defaults to showing overall averages since boot. Add 1 5 to get live averages every second for five reports.
Understanding The /Proc/Stat File
Behind the scenes, all CPU data comes from /proc/stat. You can read it directly with:
cat /proc/stat
The first line, starting with cpu, contains cumulative time spent in user, nice, system, idle, iowait, irq, softirq, steal, guest, and guest_nice modes. To calculate utilization, you need to sample twice and compute the difference.
This method is more advanced but gives you raw data for custom scripts. For example, a simple bash script can parse these values and output a percentage. It’s not recommended for casual use, but it’s powerful for automation.
Using The Vmstat Command For Cpu And Memory
vmstat provides a compact summary of system performance, including CPU. Run:
vmstat 1 5
This updates every second for five reports. The CPU columns are us (user), sy (system), id (idle), wa (wait), and st (stolen time, common in virtual machines). It’s a quick snapshot without process details.
For a more detailed view, add -w for wide output or -a for active/inactive memory. Vmstat is lightweight and works on minimal systems.
The Sar Command For Historical Data
To check CPU utilization over time, use sar from the sysstat package. It logs system activity periodically. View today’s data with:
sar -u
This shows average CPU usage for each interval (usually every 10 minutes). To see real-time updates, run:
sar -u 1 3
This gives three samples one second apart. Sar is invaluable for identifying trends or past spikes. You can also check specific dates using sar -f /var/log/sysstat/saXX where XX is the day of the month.
Using The Ps Command To Check Per-Process Cpu
Sometimes you need to see which specific process is consuming CPU. The ps command can sort by CPU usage:
ps aux --sort=-%cpu | head -10
This lists the top 10 processes by CPU percentage. The %CPU column shows the percentage of CPU time used since the process started. For real-time per-process monitoring, top or htop is better.
You can also filter by process name. For example, to check CPU usage for all nginx processes:
ps -C nginx -o pid,%cpu,cmd
This outputs PID, CPU percentage, and command line. Combine with grep for more complex queries.
Checking Cpu Utilization With The Pidstat Command
pidstat is another sysstat tool that reports per-process CPU usage. Run:
pidstat 1 3
This shows CPU usage for each active process, updating every second for three intervals. It includes columns for PID, %usr, %system, %guest, and command. It’s cleaner than ps for live monitoring.
To watch a specific process, use -p PID. For example:
pidstat -p 1234 1
This continuously updates for process 1234. Press Ctrl+C to stop.
Interpreting Cpu Utilization Output Correctly
Understanding the numbers is as important as running the commands. Here’s a quick breakdown of common terms:
- %us (user): Time spent running user-space applications.
- %sy (system): Time spent running kernel code.
- %id (idle): Time the CPU had nothing to do.
- %wa (iowait): Time waiting for I/O operations to complete.
- %st (stolen): Time stolen from a virtual machine by the hypervisor.
High %wa indicates a disk bottleneck. High %sy might mean driver issues or excessive system calls. High %st suggests your VM is overcommitted on the host.
For multi-core systems, remember that 100% utilization on one core is normal if others are idle. Use mpstat -P ALL to see per-core balance.
Common Mistakes When Checking Cpu Usage
New users often misinterpret the top output. The %CPU column in top shows CPU time as a percentage of one core. So a process using 200% means it’s using two full cores. This is normal for multi-threaded applications.
Another mistake is ignoring I/O wait. If %wa is high, your CPU isn’t actually busy; it’s waiting for data from disk or network. Fixing the storage issue will reduce apparent CPU usage.
Also, don’t rely on a single snapshot. CPU usage fluctuates. Use tools like sar or vmstat with multiple samples to get a reliable average.
Automating Cpu Utilization Checks With Scripts
You can write simple bash scripts to log CPU usage. For example:
#!/bin/bash
while true; do
echo "$(date) $(mpstat | awk '/all/ {print 100-$NF}')" >> cpu.log
sleep 5
done
This logs the idle percentage every 5 seconds. The awk command extracts the idle value and subtracts it from 100 to get utilization. Run it in the background with &.
For a more robust solution, use sar with its built-in logging. The sysstat package typically enables data collection via cron. Check /etc/cron.d/sysstat for settings.
Using The Nmon Command For Interactive Monitoring
nmon is a powerful interactive tool. Install it with:
sudo apt install nmon
Then run nmon. Press c to see CPU stats, m for memory, d for disks. It updates in real time and can save data to a file for later analysis. Nmon is great for quick, visual checks.
To record data, start nmon with -f flag. It creates a file in the current directory with timestamps. You can later analyze it with nmon_analyser or just view it in a spreadsheet.
Checking Cpu Utilization On Remote Servers
If you manage multiple servers, use SSH to run commands remotely. For example:
ssh user@server 'top -bn1 | head -5'
This runs top on the remote server and returns the first five lines. You can script this for all your servers. Tools like ansible or pssh can parallelize the task.
For continuous monitoring, consider setting up a monitoring solution like Prometheus or Nagios. But for ad-hoc checks, SSH with mpstat or sar is sufficient.
Understanding Cpu Steal Time In Virtualized Environments
If you’re on a VPS or cloud instance, watch the %st column. High steal time means your hypervisor is overcommitting CPU resources. This can cause erratic performance. Use mpstat or top to check.
For example, if %st is consistently above 10%, consider upgrading your instance or contacting your provider. It’s a sign that other VMs on the same host are hogging CPU.
Steal time is not a problem you can fix from inside the VM. It requires action from the host administrator or a move to a less crowded host.
Using The Glances Command For A Dashboard
glances is a cross-platform monitoring tool that shows CPU, memory, disk, network, and processes in one screen. Install it:
sudo apt install glances
Then run glances. It updates every second and color-codes metrics (green for good, red for critical). Press 1 to toggle per-core CPU view, q to quit.
Glances can also export data to CSV or JSON, and run as a server for remote monitoring. It’s a great all-in-one tool for sysadmins.
Comparing Different Commands: Which One To Use?
Here’s a quick guide:
- Quick check:
toporhtop - Per-core stats:
mpstat -P ALL - Historical data:
sar -u - Per-process details:
pidstatorps - Minimal output:
vmstat - Full dashboard:
glances
Choose based on your need. For scripting, mpstat and sar are easiest to parse. For interactive troubleshooting, htop or glances save time.
Real-World Example: Troubleshooting High Cpu
Imagine your web server is slow. First, run top to see overall CPU. If %wa is high, check disk I/O with iostat -x 1. If %us is high, identify the top process with ps aux --sort=-%cpu | head.
Suppose it’s a PHP-FPM process. Use pidstat -p PID 1 to monitor it. If it’s consistently using 100% of one core, investigate the PHP script. You can also use strace to see system calls.
If %st is high, contact your cloud provider. If %sy is high, check for kernel issues or excessive context switching with vmstat 1 and look at the cs column.
Using The /Cpuinfo File For Hardware Info
While not directly about utilization, knowing your CPU hardware helps. Run:
cat /proc/cpuinfo | grep "model name" | head -1
This shows the CPU model. Use nproc to see the number of cores. This info is useful when interpreting utilization percentages.
For example, if you have 4 cores and one process uses 100%, that’s 25% overall utilization. Understanding this prevents misdiagnosis.
Frequently Asked Questions
What is the easiest command to check CPU usage in Linux?
The top command is the easiest. Just type top and press Enter. It shows real-time CPU usage for all processes.
How can I check CPU utilization per core?
Use mpstat -P ALL 1 3. This displays usage for each core, updating every second for three iterations.
What does %wa mean in CPU output?
%wa stands for I/O wait. It indicates the percentage of time the CPU is idle while waiting for disk or network I/O operations to complete.
Can I check historical CPU usage?
Yes, use sar -u from the sysstat package. It shows CPU usage data logged at regular intervals. You can also specify a date with -f flag.
How do I check CPU usage of a specific process?
Use pidstat -p PID 1 to monitor a specific process. Alternatively, ps -p PID -o %cpu,cmd gives a one-time snapshot.
Final Tips For Mastering Cpu Monitoring
Practice these commands regularly. Start with top and htop, then move to mpstat and sar. Create aliases for your most-used commands. For example:
alias cpustat='mpstat -P ALL 1 5'
Add this to your .bashrc file. It saves typing and makes monitoring faster