Real-time system performance monitoring starts with checking your CPU usage in Linux. If you’re managing servers or just trying to figure out why your desktop is lagging, knowing how to check CPU usage Linux is an essential skill. This guide walks you through every major method, from simple one-liner commands to powerful interactive tools.
You don’t need to be a Linux guru to monitor your processor. In fact, most commands are intuitive once you see them in action. Let’s break down the best ways to keep an eye on your CPU, whether you’re on a headless server or a graphical desktop.
Why Monitor CPU Usage In Linux?
Before jumping into commands, it helps to understand why you’d want to check CPU usage in the first place. High CPU usage can mean a runaway process, a memory leak, or simply that your system is working hard. Low usage might indicate an idle machine or a bottleneck elsewhere.
Monitoring helps you:
- Identify processes that consume too many resources
- Troubleshoot performance slowdowns
- Optimize application performance
- Plan hardware upgrades
- Detect malicious or misbehaving software
Now let’s get into the actual methods. Each one has its own strengths, so you’ll want to know a few different approaches.
How To Check Cpu Usage Linux
The most common way to check CPU usage is with the top command. It’s pre-installed on almost every Linux distribution and gives you a live, updating view of system processes. Here’s how to use it effectively.
Using The Top Command
Open a terminal and type top. You’ll see a screen full of information. The top part shows system summary data, including CPU usage percentages. Look for lines like “%Cpu(s)” which break down user, system, idle, and other states.
Press q to quit. The display updates every few seconds by default. You can sort processes by CPU usage by pressing P (capital P). This puts the most CPU-hungry processes at the top.
Key columns to watch:
- %CPU: Percentage of CPU used by each process
- TIME+: Total CPU time consumed
- COMMAND: The name of the process
One downside of top is that it’s a bit overwhelming at first. But once you know what to look for, it’s incredibly useful.
Using Htop For A Friendlier View
If top feels too cluttered, try htop. It’s not installed by default on all systems, but you can add it with your package manager. For example, on Debian or Ubuntu, run sudo apt install htop.
Type htop and you’ll see a color-coded interface. CPU cores are displayed as bars, and processes are listed below. You can scroll vertically and horizontally, kill processes with F9, and sort by various columns.
Htop is more intuitive because it uses colors and mouse support (if enabled). It’s great for beginners and experienced users alike.
Using The Mpstat Command
For detailed per-core statistics, mpstat is your friend. It’s part of the sysstat package. Install it with sudo apt install sysstat on Debian-based systems.
Run mpstat -P ALL to see usage for each CPU core. The output shows user, nice, system, iowait, steal, idle, and other metrics. This is especially useful for identifying uneven load distribution across cores.
Example output:
Linux 5.4.0-26-generic (hostname) 03/15/2023 _x86_64_ (4 CPU)
03:45:12 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
03:45:12 PM all 12.5 0.0 2.1 0.3 0.0 0.0 0.0 0.0 85.1
03:45:12 PM 0 15.0 0.0 3.0 0.5 0.0 0.0 0.0 0.0 81.5
Checking Cpu Usage With The Ps Command
The ps command gives a snapshot of processes at a specific moment. It’s not interactive, but it’s great for scripting. To see CPU usage for all processes, run ps aux --sort=-%cpu.
This shows every process sorted by CPU usage descending. The %CPU column indicates how much CPU each process is using relative to a single core. For multi-core systems, values can exceed 100%.
You can also filter by process name. For example, ps -C firefox -o pid,%cpu,cmd shows CPU usage for Firefox only.
Using The /Proc Filesystem
For advanced users, the /proc filesystem provides raw CPU data. Run cat /proc/stat to see cumulative CPU time. The first line shows total CPU usage across all cores. You’ll see numbers for user, nice, system, idle, iowait, irq, softirq, steal, guest, and guest_nice.
To calculate CPU usage percentage, you need to sample this file twice and compute the difference. This is what tools like top do internally. It’s more work but gives you complete control.
For a quick one-liner, try grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'. This gives a rough percentage of CPU usage since boot.
Real-Time Monitoring With Watch
Sometimes you want to see a command update automatically. The watch command runs a command repeatedly and displays the output. For example, watch -n 1 'ps aux --sort=-%cpu | head -10' shows the top 10 CPU-consuming processes every second.
You can combine watch with any CPU-checking command. It’s a simple way to create a custom monitoring dashboard in your terminal.
Using System Monitor Gui Tools
If you’re on a desktop environment, graphical tools are often easier. GNOME has “System Monitor,” KDE has “KSysGuard,” and Xfce has “Task Manager.” These show CPU usage graphs, process lists, and memory usage in a friendly interface.
To open GNOME System Monitor, search for it in the activities overview or run gnome-system-monitor from the terminal. You can sort by CPU usage, kill processes, and see real-time graphs.
These tools are great for casual monitoring but lack the flexibility of command-line tools for remote servers.
Checking Cpu Usage Over Time With Sar
The sar command (part of sysstat) collects and reports system activity. It can show CPU usage history, which is invaluable for troubleshooting intermittent issues.
First, enable sysstat data collection. On Debian/Ubuntu, edit /etc/default/sysstat and set ENABLED="true". Then restart the service with sudo systemctl restart sysstat.
To view today’s CPU usage, run sar -u. For a specific date, use sar -u -f /var/log/sysstat/saDD where DD is the day of the month.
Output shows average CPU usage per interval. You can see patterns over hours or days.
Using Nmon For Interactive Monitoring
Nmon (Nigel’s Monitor) is another interactive tool. Install it with sudo apt install nmon. Run nmon and press c to see CPU stats. It shows per-core usage as bars and updates in real time.
Nmon can also log data to a file for later analysis. Press l to start logging. This is handy for long-term monitoring without keeping a terminal open.
Checking Cpu Temperature Alongside Usage
High CPU usage often leads to high temperatures. To check both, use sensors from the lm-sensors package. Install it with sudo apt install lm-sensors and run sensors.
You’ll see temperatures for each core, plus fan speeds if available. Combine this with CPU usage commands to get a full picture of system health.
For example, run watch -n 1 'echo "CPU Usage:"; top -bn1 | grep "Cpu(s)"; echo "Temperatures:"; sensors' to see both in one view.
Automating Cpu Usage Checks With Scripts
If you need to check CPU usage regularly, write a simple script. Here’s a bash script that logs CPU usage to a file every minute:
#!/bin/bash
while true; do
echo "$(date) $(top -bn1 | grep 'Cpu(s)' | awk '{print $2}')" >> cpu_log.txt
sleep 60
done
Save it as monitor_cpu.sh, make it executable with chmod +x monitor_cpu.sh, and run it in the background. You can then analyze the log later.
Understanding Cpu Usage Metrics
When you check CPU usage, you’ll see different metrics. Here’s what they mean:
- %user: Time spent running user processes
- %system: Time spent running kernel processes
- %idle: Time the CPU was idle
- %iowait: Time waiting for I/O operations
- %steal: Time stolen by hypervisor (in virtual machines)
High %iowait suggests a disk bottleneck. High %steal indicates your VM is competing for resources on the host.
Common Mistakes When Checking Cpu Usage
One common error is misinterpreting percentages. On a multi-core system, 100% CPU usage for a process means it’s fully using one core. If you have 4 cores, total CPU usage can reach 400% in top.
Another mistake is checking CPU usage only once. CPU usage fluctuates, so take multiple samples over time. Use sar or watch to see trends.
Also, remember that high CPU usage isn’t always bad. It’s only a problem if it causes slowdowns or if a process is stuck in an infinite loop.
When To Use Each Method
Here’s a quick guide:
- Quick check:
toporhtop - Per-core details:
mpstat -P ALL - Historical data:
sar -u - Scripting:
ps auxor/proc/stat - Graphical: System Monitor or KSysGuard
- Remote servers:
topover SSH
Choose the method that fits your workflow. For day-to-day monitoring, htop is often the best balance of simplicity and power.
Troubleshooting High Cpu Usage
If you find a process using excessive CPU, first identify it with top or ps. Note the PID (process ID). Then decide what to do:
- If it’s a legitimate application, consider upgrading hardware or optimizing the app.
- If it’s a system process, check for updates or misconfiguration.
- If it’s unknown, investigate further. It could be malware.
To kill a process, use kill PID. For stubborn processes, use kill -9 PID. Be careful—force-killing system processes can crash your system.
Sometimes high CPU is caused by a memory leak. Monitor memory usage alongside CPU to spot this.
Using Cgroups To Limit Cpu Usage
For advanced control, Linux control groups (cgroups) let you limit CPU usage for processes. This is useful for shared servers or containers.
To limit a process to 50% of a single core, create a cgroup and set the cpu.cfs_quota_us parameter. Tools like cpulimit provide a simpler interface. Install it with sudo apt install cpulimit and run cpulimit -l 50 -p PID to limit a process to 50% CPU.
Checking Cpu Usage On Remote Servers
When managing remote servers, you’ll use SSH. Connect with ssh user@server and then run any of the commands above. For multiple servers, consider using tools like htop over SSH or setting up a monitoring solution like Prometheus.
For a quick overview of multiple servers, you can script SSH commands. For example, for host in server1 server2; do ssh $host 'top -bn1 | head -5'; done.
Frequently Asked Questions
What is the easiest way to check CPU usage in Linux?
The easiest way is to run the top command in a terminal. It shows live CPU usage and process list. For a friendlier interface, install htop.
How do I check CPU usage per core in Linux?
Use mpstat -P ALL to see usage for each individual core. Alternatively, htop shows per-core bars by default.
Can I check CPU usage history in Linux?
Yes, use the sar command from the sysstat package. It logs CPU usage data and lets you view historical reports.
What does %CPU mean in top command output?
%CPU shows the percentage of CPU time used by a process relative to a single core. On multi-core systems, a process can exceed 100% if it uses multiple cores.
How do I check CPU usage without installing additional tools?
Use built-in commands like top, ps, or read /proc/stat directly. These are available on virtually every Linux system.
Now you have a complete toolkit for monitoring CPU usage in Linux. Start with top or htop for daily checks, and use sar for historical analysis. With practice, you’ll quickly spot performance issues and keep your system running smoothly.
Remember that CPU usage is just one piece of the performance puzzle. Combine it with memory, disk, and network monitoring for a full picture. Happy monitoring!