GICT knowledge base
Diagnosing 100% CPU Usage on Linux Server
Published
High CPU usage on a Linux server can lead to significant performance degradation, affecting applications and services running on that server. When your CPU is constantly running at 100%, it can result in slow response times, application crashes, and a frustrating experience for users. In this guide, we will explore how to diagnose and troubleshoot high CPU usage effectively on Linux servers.
Understanding CPU Usage
CPU usage is a measure of how much processing power is being utilized by the system. In Linux, CPU usage can be monitored and managed using various tools and commands. Understanding what normal CPU load looks like is essential to diagnosing when things go wrong. Typically, a moderate average CPU load without performance degradation should range between 0.5 and 1.0 on a single-core CPU, and you can multiply this number by the number of cores for multi-core CPUs.
Common Causes of High CPU Usage
There are several reasons for high CPU usage on a Linux server, including:
- Process Overload: An individual process or a group of processes may consume more CPU resources than expected, leading to a bottleneck.
- DDoS Attacks: A Distributed Denial of Service attack can lead to increased demands on server resources.
- Misconfigured Services: Services running with incorrect configurations can consume excessive CPU.
- Application Bugs: Bugs in software applications can result in infinite loops or resource-heavy operations.
- Background Services: Certain background tasks may spike CPU usage sporadically, causing temporary spikes.
- System Resource Contention: Running too many applications or services simultaneously may lead to resource contention, resulting in high CPU usage.
Tools for Diagnosing CPU Usage
To tackle high CPU usage, you will need various tools and commands at your disposal to monitor processes and diagnose issues. Here are some essential tools for a Linux server administrator:
1. top
The top command provides a dynamic, real-time view of the system’s CPU usage, displaying a list of processes consuming the most CPU. Run the command in the terminal:
top
Within the top interface, you can sort processes by CPU usage. To do this, press Shift + P to sort by CPU percentage.
2. htop
htop is an enhanced version of top that provides a color-coded, interactive output. To install htop:
sudo apt install htop # For Debian/Ubuntu
sudo yum install htop # For CentOS/RHEL
Run htop in the terminal, and you can easily sort and manage processes. Sort by CPU usage by clicking the CPU column.
3. ps
The ps command provides a snapshot of running processes. To find processes consuming CPU resources, you can customize its output:
ps aux --sort=-%cpu
This command displays a list of all processes, sorted by CPU usage in descending order.
4. mpstat
mpstat is part of the sysstat package and allows you to monitor CPU usage on a per-processor basis. Install sysstat if not already installed:
sudo apt install sysstat # For Debian/Ubuntu
sudo yum install sysstat # For CentOS/RHEL
Run mpstat to get CPU statistics over time:
mpstat -P ALL 1
This command shows CPU statistics for all available processors, updating every second.
5. iotop
If you suspect that I/O operations may be causing high CPU load, iotop helps track disk I/O by processes. To install it, run:
sudo apt install iotop # For Debian/Ubuntu
sudo yum install iotop # For CentOS/RHEL
Execute the command in the terminal:
sudo iotop
This tool will display real-time I/O usage by processes, helping you identify any misbehaving applications.
Steps to Diagnose High CPU Usage
Now that you have the tools, let’s proceed with a step-by-step approach to diagnosing high CPU usage on your Linux server:
Step 1: Monitor CPU Usage
Start by running top or htop to check the overall CPU usage. Look for processes that are consuming an unusually high percentage of CPU. Take note of the Process IDs (PIDs) and resource usage patterns.
Step 2: Review System Logs
Examine system logs to identify any unusual activity that coincides with high CPU usage. Use journalctl or check log files in the /var/log/ directory, such as /var/log/syslog or /var/log/messages:
journalctl -xe
Look for error messages or spikes in traffic correlated with CPU load.
Step 3: Use ps Command for More Detail
Run the ps command you previously learned to examine the CPU usage of processes and confirm which ones are consuming the most resources:
ps aux --sort=-%cpu | head -n 10
This command will show the top ten CPU-consuming processes.
Step 4: Check Specific Resource Usage
If you notice specific processes causing high CPU utilization, investigate those processes further. Use relevant commands to inspect their status or health, depending on the application concerned.
Step 5: Analyze I/O Operations
If it appears that heavy I/O operations are contributing to CPU load, run iotop to analyze disk usage in real time. This will help you pinpoint processes generating excessive disk activity:
sudo iotop
Troubleshooting High CPU Usage
Once you have diagnosed the source of high CPU usage, it’s time to take action.
1. Restart the Problematic Process
Sometimes, simply restarting a problematic process can alleviate high CPU usage. You can use the following command:
sudo systemctl restart [service_name]
Substitute [service_name] with the appropriate service affected by high CPU usage.
2. Configure or Limit Resource Usage
If a specific application is consistently causing high CPU usage, assess its configuration settings. For specific services like web servers or application servers, check settings that may lead to excess resource consumption. If necessary, you can limit CPU usage for specific processes using tools like cpulimit:
sudo cpulimit -e [process_name] -l [limit_percentage]
Replace [process_name] and [limit_percentage] with the appropriate values.
3. Upgrade or Optimize Software
If the high CPU usage persists due to application bugs, consider updating the software to the latest version, as newer versions may contain optimizations and bug fixes that help reduce CPU usage.
4. Analyze System Configuration
Review services running at startup and disable any unnecessary services to free up CPU resources. Use systemctl to manage services:
sudo systemctl disable [service_name]
Conclusion
Diagnosing and addressing 100% CPU usage on a Linux server is crucial for maintaining optimal performance and service availability. By employing the aforementioned tools and following a systematic approach to monitor and troubleshoot high CPU usage, you can identify and resolve issues efficiently.
Remember, maintaining a healthy server environment involves not only immediate problem-solving but also ongoing monitoring and optimization to prevent similar issues from arising in the future. By being proactive, you can ensure a stable and responsive server for all your clients and applications.
Related guides
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
