CPULimit 进阶教程

OuVPS介绍过CPULimit,用VPS挖矿必备的神器。如果不限制CPU使用率,很快VPS就会被主机商暂停,甚至是直接删除。这里关键的问题就在于,一般的VPS都是大家共享母鸡的CPU,挖矿会严重占用CPU资源,影响其他用户的使用。

《Centos中使用CPU limit限制CPU的使用率》

前面的文章中,介绍了CPULimit的基本功能,有高手写了一个shell脚本来扩展cpulimit的功能。

测试环境:

系统:ubuntu
VPS来自:《WeLoveServers – 1G 内存 | 30G硬盘 | 1T流量 G口 | 1IP | $19/年

脚本源码:

#!/bin/bash
# ==============================================================
# CPU limit daemon - set PID's max. percentage CPU consumptions
# ==============================================================

# Variables
CPU_LIMIT=20       	# Maximum percentage CPU consumption by each PID
DAEMON_INTERVAL=3  	# Daemon check interval in seconds
BLACK_PROCESSES_LIST=   # Limit only processes defined in this variable. If variable is empty (default) all violating processes are limited.
WHITE_PROCESSES_LIST=   # Limit all processes except processes defined in this variable. If variable is empty (default) all violating processes are limited.

# Check if one of the variables BLACK_PROCESSES_LIST or WHITE_PROCESSES_LIST is defined.
if [[ -n "$BLACK_PROCESSES_LIST" &&  -n "$WHITE_PROCESSES_LIST" ]] ; then    # If both variables are defined then error is produced.
   echo "At least one or both of the variables BLACK_PROCESSES_LIST or WHITE_PROCESSES_LIST must be empty."
   exit 1
elif [[ -n "$BLACK_PROCESSES_LIST" ]] ; then                                 # If this variable is non-empty then set NEW_PIDS_COMMAND variable to bellow command
   NEW_PIDS_COMMAND="top -b -n1 -c | grep -E '$BLACK_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
elif [[ -n "$WHITE_PROCESSES_LIST" ]] ; then                                 # If this variable is non-empty then set NEW_PIDS_COMMAND variable to bellow command
   NEW_PIDS_COMMAND="top -b -n1 -c | gawk 'NR>6' | grep -E -v '$WHITE_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
else
   NEW_PIDS_COMMAND="top -b -n1 -c | gawk 'NR>6 && \$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
fi

# Search and limit violating PIDs
while sleep $DAEMON_INTERVAL
do
   NEW_PIDS=$(eval "$NEW_PIDS_COMMAND")                                                                    # Violating PIDs
   LIMITED_PIDS=$(ps -eo args | gawk '$1=="cpulimit" {print $3}')                                          # Already limited PIDs
   QUEUE_PIDS=$(comm -23 <(echo "$NEW_PIDS" | sort -u) <(echo "$LIMITED_PIDS" | sort -u) | grep -v '^$')   # PIDs in queue

   for i in $QUEUE_PIDS
   do
       cpulimit -p $i -l $CPU_LIMIT -z &   # Limit new violating processes
   done
done

使用说明:
这个脚本的使用需要用到gawk

1.安装cpulimit和gawk

sudo apt-get install cpulimit gawk

2.下载上面的脚本,也可以自己新建一个文件把上面的源码拷贝过去

wget http://www.ouvps.com/sh/cpulimit_daemon.sh -O /usr/bin/cpulimit_daemon.sh

3.脚本修改

上面脚本的详细解释

CPU_LIMIT: cpu的最大使用率
DAEMON_INTERVAL: 监测CPU占用率的时间间隔
BLACK_PROCESS_LIST: 限制CPU使用率的软件
WHITE_PROCESSES_LIST: 不限制CPU使用率的软件
black list和white list只能同时启用一个,不能同时使用,白名单和黑名单必须有一个是空的。白名单和黑名单中有多个程序时,用竖线连接,比如“mysql|firefox|gedit”

4.修改脚本权限

sudo chmod 755 /usr/bin/cpulimit_daemon.sh

5.用脚本控制cpulimit

wget http://www.ouvps.com/sh/cpulimit -O /etc/init.d/cpulimit

有了这个脚本就可以通过service服务的方式来管理cpulimit的后台进程

6.修改cpulimit的组

sudo chown root:root /etc/init.d/cpulimit

7.修改cpulimit的权限

sudo chmod 755 /etc/init.d/cpulimit

8.把cpulimit添加到开机启动

sudo update-rc.d cpulimit defaults

9.重启系统,测试cpulimit是否成功添加到自启动

sudo reboot

10.管理cpulimit

sudo service cpulimit status #查看cpulimit的状态
sudo service cpulimit start #启动cpulimit
sudo service cpulimit stop #终止cpulimit

Add a Comment

邮箱地址不会被公开。 必填项已用*标注