Understanding which processes consume the most processing power begins with checking CPU usage on Linux. If you’re managing a server or just troubleshooting a slow desktop, knowing How To Check Cpu Usage On Linux is a fundamental skill. This guide will walk you through every method, from simple commands to real-time monitoring tools.
Linux gives you many ways to see what your CPU is doing. Some methods are quick and give you a snapshot, while others show live updates. We’ll cover both so you can pick what works best for your situation.
Why Checking CPU Usage Matters
High CPU usage can slow down your system or even crash it. By monitoring CPU usage, you can spot runaway processes, optimize performance, and plan for hardware upgrades. It’s like checking your car’s engine temperature—you catch problems before they get serious.
You might need to check CPU usage when your system feels sluggish, a server stops responding, or you’re debugging an application. The tools we’ll discuss work on most Linux distributions, including Ubuntu, Debian, CentOS, and Fedora.
How To Check Cpu Usage On Linux
Let’s dive into the most common methods. Each approach has its strengths, so try a few to see which one fits your workflow.
Using The Top Command
The top command is the go-to tool for many Linux users. It shows a live, updating list of processes sorted by CPU usage. Open your terminal and type:
top
You’ll see a screen with several sections:
- Summary area at the top shows total CPU usage, load average, and memory stats.
- Process list below shows each process with its PID, user, CPU%, and more.
Press q to quit. The default view updates every few seconds. To change the refresh rate, press d and enter a new delay in seconds.
One handy trick: press P (capital P) to sort processes by CPU usage descending. This quickly shows you which process is hogging the most power.
Using The Htop Command
If top feels a bit plain, try htop. It’s a more colorful, user-friendly version. You might need to install it first:
sudo apt install htop # Debian/Ubuntu
sudo yum install htop # CentOS/RHEL
Then run:
htop
Htop shows CPU usage as a bar graph for each core. You can scroll through processes, kill them with F9, or change the sort order with F6. It’s more intuitive for beginners.
Unlike top, htop lets you select multiple processes and send signals to them. This makes it great for managing runaway processes.
Using The Ps Command
The ps command gives you a snapshot of current processes. It doesn’t update live, but it’s useful for scripting or one-time checks. To see CPU usage for all processes, run:
ps aux
The output includes a column called %CPU. This shows the percentage of CPU time used since the process started. For a more accurate view, use:
ps aux --sort=-%cpu
This sorts processes by CPU usage from highest to lowest. You can pipe this to head to see only the top 10:
ps aux --sort=-%cpu | head -11
Using The Mpstat Command
If you want detailed per-core CPU statistics, mpstat is your friend. It’s part of the sysstat package. Install it if needed:
sudo apt install sysstat
Then run:
mpstat -P ALL 1
This shows CPU usage for each core, updating every second. The output includes user, system, idle, and iowait percentages. It’s perfect for spotting if one core is maxed out while others are idle.
Using The Sar Command
The sar command collects and reports system activity over time. It’s great for historical analysis. To see CPU usage for today, run:
sar -u
This shows averages for the day so far. To see real-time updates every 2 seconds for 5 times:
sar -u 2 5
Sar is powerful for trend analysis. You can check CPU usage from yesterday or last week by specifying the data file.
Using The /Proc Filesystem
For the truly curious, Linux exposes CPU stats in the /proc filesystem. Check /proc/stat for overall CPU usage:
cat /proc/stat
Look for the line starting with cpu. The numbers represent time spent in user, nice, system, idle, and other states. You can calculate CPU usage by comparing two samples.
This method is more complex but gives you raw data. It’s useful for custom monitoring scripts.
Real-Time Monitoring With Graphical Tools
If you prefer a GUI, Linux has several options. These are especially handy on desktop systems.
Using Gnome System Monitor
On Gnome desktops, search for “System Monitor” in the activities overview. It shows CPU usage as a graph, along with memory and network activity. You can sort processes and kill them with a click.
Using KDE System Guard
KDE users can use KSysGuard. It provides real-time graphs for CPU, memory, and disk usage. You can add custom sensors for more detailed monitoring.
Using Conky
Conky is a lightweight system monitor that runs on your desktop. It’s highly customizable. You can display CPU usage, temperature, and more. It’s popular among Linux enthusiasts who want a stylish desktop.
Interpreting CPU Usage Data
Knowing how to check CPU usage is one thing; understanding the numbers is another. Here’s what to look for:
- User CPU: Time spent running user processes (your apps).
- System CPU: Time spent running kernel processes (system calls).
- Idle CPU: Time the CPU has nothing to do.
- Iowait CPU: Time waiting for input/output operations (disk or network). High iowait means your storage is slow.
If CPU usage is consistently above 80%, you might need to optimize processes or upgrade hardware. If iowait is high, check your disk performance.
Common Scenarios And Solutions
High CPU Usage From A Single Process
If one process is using 100% CPU, it could be a bug or a malicious script. Use top or htop to find the PID. Then investigate:
ps -p PID -o comm,pid,user
If it’s a legitimate process, consider restarting it. If it’s suspicious, kill it with kill -9 PID.
High System CPU Usage
High system CPU often means driver issues or kernel problems. Check for recent updates or hardware changes. Use mpstat to see if it’s consistent across cores.
High Iowait CPU Usage
If iowait is high, your disk is the bottleneck. Check disk activity with iostat or iotop. Consider upgrading to an SSD or optimizing your storage layout.
Automating CPU Usage Monitoring
You can set up scripts to monitor CPU usage and alert you when it’s too high. Here’s a simple bash script using top:
#!/bin/bash
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}')
if (( $(echo "$CPU > 90" | bc -l) )); then
echo "CPU usage is high: $CPU%"
fi
Run this with cron to check every few minutes. You can extend it to send emails or log data.
Using Systemd Tools
Modern Linux systems use systemd. You can check CPU usage for services with:
systemd-cgtop
This shows control groups and their resource usage. It’s useful for containers or services managed by systemd.
Comparing Tools: Which One To Use?
Here’s a quick comparison to help you choose:
| Tool | Best For | Live Update |
|---|---|---|
| top | Quick checks, basic monitoring | Yes |
| htop | User-friendly, interactive | Yes |
| ps | Scripting, one-time snapshots | No |
| mpstat | Per-core analysis | Yes |
| sar | Historical data, trend analysis | No |
| /proc | Custom scripts, raw data | No |
Start with top or htop for daily use. Use mpstat when you need core-level detail. Sar is your friend for long-term monitoring.
Advanced Tips For Power Users
If you’re comfortable with the command line, try these advanced techniques:
- Use watch:
watch -n 1 'ps aux --sort=-%cpu | head'updates every second. - Use awk: Parse
/proc/statto calculate CPU usage in scripts. - Use perf: The
perftool gives deep CPU profiling for developers.
These methods give you more control but require more knowledge. Start with the basics and build up.
Frequently Asked Questions
What Is The Easiest Way To Check CPU Usage On Linux?
The easiest way is to run the top command in the terminal. It shows a live list of processes sorted by CPU usage. Just type top and press Enter.
How Do I Check CPU Usage Per Core On Linux?
Use the mpstat -P ALL 1 command. This shows CPU usage for each core, updating every second. You need the sysstat package installed.
Can I Check Historical CPU Usage On Linux?
Yes, use the sar command. Run sar -u to see today’s averages, or specify a time range with sar -u -s 08:00:00 -e 17:00:00.
What Does High Iowait CPU Mean?
High iowait means the CPU is waiting for disk or network operations to complete. It often indicates a slow storage device. Check disk performance with iostat.
How Do I Kill A Process Using Too Much CPU?
Find the process ID (PID) with top or ps, then run kill -9 PID. Use kill -15 for a gentler shutdown first.
Putting It All Together
Now you have a complete toolkit for checking CPU usage on Linux. Start with top for quick checks, then explore htop for a better interface. Use mpstat for per-core details and sar for history.
Remember, monitoring CPU usage is just one part of system health. Combine it with memory and disk monitoring for a full picture. Practice these commands on a test system until they become second nature.
If you run into trouble, the man pages are your friend. Type man top or man htop for detailed documentation. Most Linux communities are also helpful if you get stuck.
You now have the skills to diagnose CPU-related issues on any Linux system. Whether you’re a beginner or an experienced admin, these tools will serve you well. Go ahead and try them out—your system will thank you.