Use cpulimit to limit VPS CPU usage and prevent suspend

Use cpulimit to limit VPS CPU usage and prevent suspend

Preface

Because machines with small memory are often cheaper, the annual payment may be around $5, so manufacturers will limit the CPU and disk IO. If it exceeds the limit, it will be suspended. This is a casual thing, and some host companies will not provide refunds for suspend.

So we can use some software to limit CPU usage and IO. Also, don't use any benchmarking software to run the game, otherwise you're done for.

Install

Cpulimit is a software that can control CPU usage.

Package Installation

Centos:

 yum install cpulimit

Debian/Ubuntu

 apt-get install cpulimit

Compile and install

 cd /tmp wget 'http://downloads.sourceforge.net/cpulimit/cpulimit-1.1.tar.gz' tar cpulimit-1.1.tar.gz cd cpulimit-1.1 make cp cpulimit /usr/local/sbin/ rm -rf cpulimit*

use

illustrate

 cpulimit -h Usage: cpulimit [OPTIONS...] TARGET OPTIONS -l, --limit=N percentage of cpu allowed from 0 to 100 (required)//cpu限制的百分比-v, --verbose show control statistics//顯示版本號-z, --lazy exit if there is no target process, or if it dies//如果限制的進程不存在了,則退出。 -i, --include-children limit also the children processes//包括子進程。 -h, --help display this help and exit //幫助,顯示參數TARGET must be exactly one of these: -p, --pid=N pid of the process (implies -z) //進程的pid -e, --exe=FILE name of the executable program file or path name //可執行程序COMMAND [ARGS] run this command and limit it (implies -z)

usage

Limit Firefox to 30% CPU utilization

 cpulimit -e firefox -l 30

Limit the program with process number 1313 to 30% CPU utilization

 cpulimit -p 1313 -l 30

Limit the CPU utilization of the software under the absolute path

 cpulimit -e /usr/local/nginx/sbin/nginx -l 50

Precautions

The CPU usage limit after -l should be reduced exponentially according to the actual number of cores. The 40% limit is effective for a single-core server. If it is a dual-core server, it should be limited to 20%, a quad-core server to 10%, and so on.
The root user can restrict all processes, while ordinary users can only restrict processes that they have permission to manage.
Limit CPU usage for all processes

By default, cpulimit can only limit existing processes, but you can set this script to start automatically at random (see the script link above for setting methods). It will monitor and limit all processes (including new processes) (check once every 3 seconds, CPU limit is 75%)

This can prevent the system from being suspended due to high CPU usage!

 #!/bin/bash while true ; do id=`ps -ef | grep cpulimit | grep -v "grep" | awk '{print $10}' | tail -1` nid=`ps aux | awk '{ if ( $3 > 75 ) print $2 }' | head -1` if [ "${nid}" != "" ] && [ "${nid}" != "${id}" ] ; then cpulimit -p ${nid} -l 75 & echo "[`date`] CpuLimiter run for ${nid} `ps -ef | grep ${nid} | awk '{print $8}' | head -1`" >> /root/cpulimit-log.log fi sleep 3 done

Save to /root/cpulimit.sh, and the log file /root/cpulimit-log.log will be automatically generated

Then add this cost reduction to start at boot.

Set to start at boot

Modify /etc/rc.local and add /root/cpulimit.sh to the corresponding position and restart the system, which will limit the CPU usage of each process!

via: https://www.mf8.biz/cpulimit-make-none-suspend/

<<:  FastWebHost: $12/year/unlimited space/unlimited traffic/can bind 1 domain name/Phoenix

>>:  K9VPS: $5.94/year/128MB memory/10GB space/500GB traffic/OpenVZ/Los Angeles

Recommend

Hostodo: $30/year/512MB memory/15GB space/1TB traffic/KVM/Dallas/Los Angeles

Hostodo, a low-cost VPS merchant, recently launch...

ipsystemsltd: $6/year/512MB RAM/10GB SSD/3 IPs/500GB traffic/OpenVZ

IP Systems Limited is a newly established America...

Hostodo: $15/year/512MB memory/15GB space/1TB traffic/KVM/Los Angeles/Dallas

Hostodo has launched 3 offers, as follows: KVM-51...

Recommend a WordPress theme site with rich resources

1. http://wordpress.org.cn There are many Chinese...

$2/month/128MB RAM/10GB storage/250GB bandwidth/OpenVZ —— miniVPS

miniVPS is a British hosting company founded in 2...

W3Space: $15/year/1GB RAM/25GB SSD space/1.5TB traffic/1Gbps port/KVM/France

W3Space is a foreign business founded in 2013. It...

Cloudflare Pages: Unlimited traffic, unlimited sites, global CDN

Cloudflare Pages is a new product launched by Clo...

DGCHost 512MB RAM 30Mbps Port Hong Kong KVM VPS Review

Details: DGCHost: $4.5/month/512MB memory/20GB SS...

Archive: A global website history museum

Archive, a global website history museum, allows ...