Check CPU Usage in Linux
Vivek Yadav
Full Stack Developer | JavaScript & Python | Passionate about Web Development
Monitoring CPU usage in Linux is crucial for system optimization, troubleshooting performance issues, and ensuring efficient resource utilization. Whether you're a system administrator, developer, or a regular user, understanding how to check CPU usage in Linux can help you maintain system stability and performance.
High CPU usage can slow down applications, cause overheating, and lead to unexpected crashes. By leveraging built-in commands and third-party tools, you can efficiently monitor CPU performance and take necessary actions to optimize your Linux system.
How to Check CPU Usage in Linux (Overview)
There are multiple ways to check CPU usage in Linux, including command-line utilities and graphical tools. Here’s an overview of the most effective methods:
Below, we explore each method in detail with usage examples.
Also Read:- install chrome for linux
Using the top Command
The top command is one of the most commonly used tools to check CPU usage in real time.
How to Use top Command:
Open a terminal and run:
top
This command displays an updated list of processes, including CPU and memory usage.
Understanding top Output:
To exit top, press q.
Also Read:- how to install splunk on linux
Using the htop Command (More User-Friendly Alternative)
htop is an enhanced, interactive version of top with an easier-to-read interface.
Installing htop:
sudo apt install htop # Debian/Ubuntu
sudo yum install htop # RHEL/CentOS
sudo pacman -S htop # Arch Linux
Running htop
htop
htop provides a visually appealing, color-coded representation of CPU usage with an intuitive navigation system.
Checking CPU Usage with mpstat (Advanced Users)
The mpstat command (part of the sysstat package) provides CPU usage statistics for each processor core.
Installing mpstat
sudo apt install sysstat # Debian/Ubuntu
sudo yum install sysstat # RHEL/CentOS
Using mpstat to Monitor CPU Usage:
mpstat 5 10
This command samples CPU usage every 5 seconds for 10 iterations.
Using sar for Historical CPU Usage
The sar command helps track CPU performance trends over time.
Installing sar
sudo apt install sysstat # Debian/Ubuntu
Checking CPU Usage with sar
sar -u 5 10
This command prints CPU utilization every 5 seconds for 10 cycles.
Monitoring CPU Usage with iostat
The iostat command provides reports on CPU and disk activity.
Installing iostat
sudo apt install sysstat # Debian/Ubuntu
Using iostat
iostat -c 5 10
This displays CPU usage statistics every 5 seconds for 10 iterations.
Also Read:- how to turn on linux on chromebook
Graphical Tools for Checking CPU Usage in Linux
For users who prefer GUI-based monitoring, Linux provides various tools:
These tools offer a more user-friendly approach to CPU monitoring, displaying graphical statistics and trends over time.
How to Reduce High CPU Usage in Linux
If your system experiences high CPU usage, consider the following:
Identify and Kill High CPU Processes
Run:
top
Locate the high CPU-consuming process and terminate it:
kill -9 <PID>
Adjust Process Priorities
Lower the priority of a process using nice:
nice -n 10 <command>
Or change priority of an existing process:
renice -n 10 -p <PID>
Disable Unnecessary Services
List running services:
systemctl list-units --type=service
Disable unwanted services:
sudo systemctl disable <service-name>
Optimize System Performance
Frequently Asked Questions (FAQs)
How do I check real-time CPU usage in Linux?
You can check real-time CPU usage in Linux using the top or htop commands. top provides a basic real-time view, while htop offers a more user-friendly interface.
How can I monitor CPU usage per core in Linux?
The mpstat -P ALL command displays CPU usage per core. You can also use htop, which visually separates CPU usage for each core.
How do I log CPU usage over time?
Use the sar command to log CPU usage trends over time. For example:
sar -u 5 100 > cpu_log.txt
This logs CPU usage every 5 seconds for 100 iterations.
What should I do if my CPU usage is too high?
What is a normal CPU usage percentage in Linux?
Normal CPU usage varies depending on workload. Idle systems should be below 10%, while active workloads may reach 50-70%. Sustained usage over 90% may indicate performance issues.
Conclusion
Monitoring CPU usage in Linux is essential for maintaining optimal system performance. Whether using command-line tools like top, htop, mpstat, or GUI-based solutions like GNOME System Monitor, each method offers unique advantages.
By proactively checking CPU usage and optimizing performance, you can ensure your Linux system runs efficiently. Try out these commands and tools to monitor and manage your system resources effectively!