VPS Tutorials
VPS CPU at 100%? A Performance Optimization Troubleshooting Guide
What should you do when VPS CPU usage hits 100%? This article explains top/htop diagnostic methods, techniques for finding high-CPU processes, analysis of common causes (runaway scripts, database overload, mining intrusions), plus process management and long-term monitoring solutions.

Your VPS suddenly becomes very slow, SSH connections lag, and website response times spike? Check CPU usage — it has probably hit 100%. This situation is more common than you might think, but as long as you master the right troubleshooting methods, you can usually pinpoint and resolve the problem quickly.
First, log in to your VPS and run the most basic command to see the current status:
top
The top command displays system resource usage in real time. Pay attention to these values:
- %Cpu(s) - Overall CPU usage
- us - Usage by user processes
- sy - Usage by the system kernel
- id - Idle percentage (a very low value here means the CPU is very busy)
Press the P key to sort by CPU usage and quickly find which process is consuming resources. Press q to exit.
If you want a friendlier interface, you can install htop:
# Debian/Ubuntu
apt install htop -y
# CentOS/RHEL
yum install htop -y
# 运行
htop
htop supports mouse operation and color display, making it more intuitive to read.
ps aux --sort=-%cpu | head -20
This lists the top 20 processes by CPU usage. The meaning of each column in the output:
- USER - The user running the process
- PID - Process ID
- %CPU - CPU usage percentage
- %MEM - Memory usage percentage
- COMMAND - The process command
watch -n 1 'ps aux --sort=-%cpu | head -10'
Refreshes once per second so you can observe trends in CPU usage.
After finding the PID of a suspicious process, look at its details:
# 查看进程的完整命令行
cat /proc/<PID>/cmdline | tr '\0' ' '
# 查看进程的工作目录
ls -l /proc/<PID>/cwd
# 查看进程打开的文件
lsof -p <PID>
Infinite loops or inefficient queries in website code are the most common cause.
Characteristics: php-fpm or python processes continuously at 100% CPU
Troubleshooting:
# 查看 PHP-FPM 进程状态
ps aux | grep php-fpm
# 查看正在执行的 PHP 脚本
lsof -c php-fpm | grep '\.php'
Solution:
- Check recently modified code
- Check the PHP error log
/var/log/php-fpm/error.log - Temporarily restart PHP-FPM:
systemctl restart php-fpm
Slow MySQL queries can bring down the entire server.
Characteristics: The mysqld process uses very high CPU
Troubleshooting:
# 登录 MySQL 查看当前查询
mysql -e "SHOW PROCESSLIST;"
# 查看慢查询日志(需要先开启)
tail -f /var/log/mysql/slow.log
Solution:
- Optimize slow queries and add indexes
- Kill stuck queries:
mysql -e "KILL <process_id>;" - Consider upgrading the VPS configuration or using a dedicated database server
This is a serious problem that needs immediate handling.
Characteristics:
- Unfamiliar process names, such as
kdevtmpfsi,kinsing, or random strings - Processes are hidden or cannot be killed normally
- CPU stays at 100% but you cannot find the cause
Troubleshooting:
# 检查定时任务
crontab -l
cat /etc/crontab
ls -la /etc/cron.d/
# 检查可疑的启动项
systemctl list-unit-files --type=service | grep enabled
# 检查异常的网络连接
netstat -antp | grep ESTABLISHED
Solution:
- Kill the suspicious processes immediately
- Clean up malicious entries in scheduled tasks
- Change all passwords and check SSH keys
- Update the system and all software
- In severe cases, reinstalling the system is recommended
Too many Nginx or Apache workers are configured, or the PHP-FPM process pool is set too large.
Troubleshooting:
# 查看 Nginx worker 数量
ps aux | grep nginx | wc -l
# 查看 PHP-FPM 进程数量
ps aux | grep php-fpm | wc -l
Solution: Adjust parameters according to the VPS configuration. For a 1-core VPS, recommended values:
- Nginx workers: 1-2
- PHP-FPM pm.max_children: 5-10
Background system tasks may temporarily saturate the CPU.
Characteristics: High CPU in processes such as apt, yum, rsync, gzip
Solution: Usually you just need to wait for the task to finish. If it is not urgent, you can lower the priority:
# 降低进程优先级(19 是最低)
renice 19 -p <PID>
# 正常终止
kill <PID>
# 强制终止(进程无响应时使用)
kill -9 <PID>
# 按进程名批量杀
pkill -f <进程名关键词>
Use the cpulimit tool to limit the CPU usage of a specific process:
# 安装
apt install cpulimit -y
# 限制某进程最多使用 50% CPU
cpulimit -p <PID> -l 50
# 启动时设置低优先级
nice -n 19 <命令>
# 修改运行中进程的优先级
renice 10 -p <PID>
# 安装 nmon
apt install nmon -y
nmon
# 或者安装 glances(功能更全)
pip3 install glances
glances
Write a simple monitoring script that sends a notification when CPU exceeds a threshold:
#!/bin/bash
CPU_THRESHOLD=90
CPU_USAGE=$(top -bn1 | grep 'Cpu(s)' | awk '{print int($2)}')
if [ $CPU_USAGE -gt $CPU_THRESHOLD ]; then
echo "CPU 使用率 ${CPU_USAGE}% 超过阈值" | mail -s "VPS CPU 告警" [email protected]
fi
Add it to crontab to check every 5 minutes:
*/5 * * * * /path/to/cpu_monitor.sh
If the CPU frequently hits 100%, besides optimizing code and configuration, you may need to consider:
- Upgrade the VPS configuration - Increase the number of CPU cores
- Use a higher-frequency CPU - Some providers offer high-performance instances
- Separate services - Move databases, caches, etc. to dedicated servers
- Add a caching layer - Redis and Memcached reduce computing pressure
- Use a CDN - Reduce the server's static resource load
Troubleshooting workflow for CPU at 100%:
- Use
toporhtopto confirm CPU status - Use
ps aux --sort=-%cputo find high-usage processes - Analyze the process type to determine whether it is a normal task or an anomaly
- Handle it accordingly: optimize code, adjust configuration, kill malicious processes
- Set up long-term monitoring to prevent the problem from recurring
In most cases, following this workflow lets you pinpoint the problem quickly. If you really cannot find the cause, consider stopping services one by one to isolate it, or simply restart the VPS to temporarily recover and then investigate slowly.