✅ 一键防CPU被压死防护脚本(管理员PowerShell运行)

Write-Host "正在配置服务器防CPU压死防护..." -ForegroundColor Cyan;
# 1. 开启系统CPU资源保护,限制单进程CPU占用
New-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Power" -Force | New-ItemProperty -Name HiberbootEnabled -Value 0 -PropertyType DWord -Force;
New-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Processor\Parameters" -Force | New-ItemProperty -Name DisableIdleStates -Value 1 -PropertyType DWord -Force;
# 2. 限制Nginx/PHP进程CPU占用上限(防止恶意请求吃满CPU)
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\nginx.exe\PerfOptions" -Force | New-ItemProperty -Name CpuPriorityClass -Value 4 -PropertyType DWord -Force;
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\php-cgi.exe\PerfOptions" -Force | New-ItemProperty -Name CpuPriorityClass -Value 4 -PropertyType DWord -Force;
# 3. 开启CPU节流保护,高负载时自动限制非核心进程
New-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\54533251-82be-4824-96c1-4b7fc84815f8\0cc5b647-c1df-4637-891a-dec35c318583" -Force | New-ItemProperty -Name Attributes -Value 2 -PropertyType DWord -Force;
# 4. 禁用系统后台CPU占用服务
Stop-Service -Name "WSearch", "SysMain", "wuauserv" -Force -ErrorAction SilentlyContinue; Set-Service -Name "WSearch", "SysMain", "wuauserv" -StartupType Disabled -ErrorAction SilentlyContinue;
# 5. 开启自动CPU守护,高负载时清理闲置进程
schtasks /Create /TN "AutoCpuGuard" /TR "powershell.exe -Command \"Get-Process | Where-Object {$_.Cpu -gt 50 -and $_.Name -notin ('nginx','php-cgi','mysql','System')} | Stop-Process -Force -ErrorAction SilentlyContinue\"" /SC MINUTE /MO 1 /RL HIGHEST /F >nul 2>&1;
Write-Host "✅ 防CPU压死防护配置完成!高负载下CPU也不会被吃满" -ForegroundColor Green
✅ 一键防CPU被压死防护脚本(管理员PowerShell运行)
© 版权声明
THE END
暂无评论内容