High CPU usage (99.5%) indicates that your server is heavily utilizing its CPU resources. Here are steps to diagnose and address the issue:
1. Identify the Processes Using CPU
Use the top or htop command to identify processes consuming the most CPU:
top
Press P to sort by CPU usage.
htop (requires installation):
sudo apt-get install htop
htop
In htop, you can sort by CPU usage and get a clearer view of what's consuming resources.
2. Investigate High CPU Processes
Once you identify the processes consuming high CPU:
Check the Process:
Use ps to get more details about a specific process:
ps aux | grep <process_id>
Replace <process_id> with the actual process ID you found using top or htop.
Examine Logs:
Check system logs for any errors or warnings that might indicate why a process is using excessive CPU:
sudo less /var/log/syslog
sudo less /var/log/messages
3. Restart or Stop the Process
If a specific process is misbehaving or causing high CPU usage:
Restart the Service:
You can restart the service if it’s related to a service:
sudo systemctl restart <service_name>
Replace <service_name> with the relevant service name.
Stop the Process:
If the process doesn’t need to be running, you can stop it:
sudo kill -9 <process_id>
Use kill -15 <process_id> for a more graceful stop if possible.
4. Analyze Resource Usage
Check for patterns or resource-heavy applications:
Check Running Services:
Ensure that no unnecessary services are running.
Disable or remove services that are not needed.
Review Scheduled Tasks:
Ensure there are no misconfigured cron jobs or scheduled tasks causing high CPU usage:
crontab -l
sudo less /etc/crontab
5. Optimize and Tune
Consider optimizing the applications or services consuming high CPU:
Application Configuration:
Review the configuration settings for applications. Sometimes, increasing resources or optimizing configurations can reduce CPU usage.
Update Software:
Ensure that all your software and services are up-to-date, as updates may include performance improvements.
6. Add More Resources
If high CPU usage is a constant issue due to legitimate load:
Upgrade Server Resources:
Consider upgrading to a server with more CPU cores or higher performance.
Load Balancing:
Implement load balancing or distribute the load across multiple servers.
7. Monitor and Maintain
Set up monitoring tools to keep track of CPU usage and other system metrics to prevent future issues:
Install Monitoring Tools:
Tools like Nagios, Zabbix, Prometheus, or Grafana can help you monitor CPU usage and other system metrics.
By following these steps, you can diagnose and address the high CPU usage on your server.