Description: We often have a lot of cheap hands VPS
, sometimes use some software applications, there will be a CPU
run full situation, and long time to run full will be VPS
stopped by the merchant, so here we need to find ways to limit CPU
The process usage, here is a tutorial.
Brief introduction
cpulimit
The command works by cpu
setting a maximum occupancy limit for a process and monitoring the process to see if it exceeds this limit in real time, making dynamic adjustments. This allows you to control the cpu
upper value of the process's usage rate.
Installation
Use the root
Run command:
#debian/-y cpulimit#rhel/centos/Fedora system yum install Epel-release cpulimit
Use
CPULimit-Husage:cpulimit [OPTIONS ...] TARGET OPTIONS-L,--limit=n percentage of CPU allowed from 0To -(required)//percent of CPU limit-V,--verbose show control statistics//Show version number-Z,--lazy exitifThere isNo target process, orifIt dies//if the restricted process does not exist, exit. -I,--include-children limit also the children processes//include child processes. -H,--help display ThisHelp and exit//help, displaying parametersTARGET must be exactly one of these:-P,--pid=n pid of the process (IMPLIES-Z)//PID of the process-E,--exe=file name of the executable program FILE or path name//executable programCOMMAND [ARGS] Run Thiscommand and limit It (implies-z)
Usage
1. General usage
#限制firefox使用30%#限制进程号1313的程序使用30131350
2. Limit CPU usage for all processes
By default cpulimit
, only processes that already exist are restricted, but the script is set to random self-boot, and it monitors and restricts all processes (including the new process) ( 3
once in seconds, CPU
limited to 75%
)
This prevents CPU
the usage from being too high ban
!
#!/bin/Bash while true; DoID= ' Ps-ef | grep CPULimit | Grep-v"grep"| Awk'{print $}'| Tail-1' Nid= ' PS aux | Awk'{if ($ >) Print $'| Head-1` if["${nid}"!=""] && ["${nid}"!="${id}" ] ; Then CPULimit-P ${nid}-L the&Echo"[' Date '] cpulimiter run for ${nid} ' ps-ef | grep ${nid} | awk ' {print $8} ' | head-1 '">>/root/cpulimit-log.log fi sleep3 Done
Saved to /root/cpulimit.sh
, the log file is automatically generated /root/cpulimit-log.log
.
Then modify /etc/rc.local
the corresponding location to join and restart the /root/cpulimit.sh
system, will be the whole process of restricting the use of various processes CPU
!
Precautions
L, the latter limit of the CPU usage, according to the actual number of cores to be reduced exponentially. the 1-core server, if it is a dual-core server, should be limited to 20%, the quad-core server is limited to 10%, and so on . 2, the root user can restrict all processes, ordinary users can only restrict their own rights to manage the process.
Linux VPS To limit CPU usage by installing CPULimit