Linux VPS通过安装CPULimit来限制CPU使用率

说明:我们手上经常有很多廉价的VPS,有时候使用某些软件应用的时候,会出现CPU跑满的情况,而长时间跑满会被VPS商家停掉,所以这里我们需要想办法来限制进程CPU使用率,这里就说个教程。

简介

cpulimit命令的工作原理是为进程预设一个cpu占用率上限,并实时监控进程是否超出此上限,而做出动态调整。从而可以控制进程的cpu使用率的上限值。

 

安装

使用root运行命令:

#debian/ubuntu系统
apt install -y cpulimit

#RHEL/Centos/Fedora系统
yum install epel-release cpulimit
 

使用

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)

 

 

用法

1、常规用法

#限制firefox使用30% cpu利用率
cpulimit -e firefox -l 30

#限制进程号1313的程序使用30%cpu利用率
cpulimit -p 1313 -l 30

#限制绝对路径下该软件的cpu利用率
cpulimit -e /usr/local/nginx/sbin/nginx -l 50

 

2、限制所有进程的CPU使用率
默认情况下cpulimit只能对已经存在的进程进行限制,但是设置此脚本为随机自启动即可,它会对所有进程(包括新建进程)进行监控并限制(3秒检测一次,CPU限制为75%

这就可以防止因为CPU使用率过高而被ban了!

#!/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

 

保存到 /root/cpulimit.sh,会自动生成日志文件 /root/cpulimit-log.log

然后修改 /etc/rc.local 在对应位置加入 /root/cpulimit.sh 再重启系统,就会全程限制各个进程的CPU使用了!

 

注意事项

l、后面限制的cpu使用量,要根据实际的核心数量而成倍减少。40%的限制生效在1核服务器中,如果是双核服务器,则应该限制到20%,四核服务器限制到10%以此类推。
2、root用户可以限制所有的进程,普通用户只能限制自己有权限管理的进程。

 

转载于:https://www.cnblogs.com/lxh823/p/8612562.html

`psutil`(process and system utilities)是一个跨平台库,用于获取系统运行时的信息,包括CPU使用率、内存使用情况、磁盘I/O、网络I/O等信息。但是需要注意的是,`psutil`本身并不提供限制CPU使用率的功能。限制CPU使用率通常需要使用操作系统的特定功能或第三方库来实现。 如果你想限制Python程序的CPU使用率,可以通过创建多个进程来模拟限制效果,每个进程运行相同的任务,但是控制它们同时运行的数量。然而,这种方式并不是直接限制单个进程的CPU使用率,而是通过限制并发进程的数量来间接达到效果。 此外,对于Windows系统,可以通过调用Windows的作业对象(Job Object)API来限制CPU使用率,而对于Linux系统,则可以使用`nice`命令或者通过设置`cpulimit`工具来限制进程的CPU使用率。 如果你确实需要在Python中限制CPU使用率,可以考虑使用`concurrent.futures.ProcessPoolExecutor`来管理多个进程,并结合某种形式的信号量或其他同步机制来控制同时运行的进程数量,从而限制整体的CPU使用率。 这里有一些示例代码,展示如何使用`concurrent.futures`来创建和管理进程池: ```python from concurrent.futures import ProcessPoolExecutor, as_completed def my_function(): # 这里是需要执行的任务 pass # 定义要启动的进程数量 max_workers = 4 # 创建进程池 with ProcessPoolExecutor(max_workers=max_workers) as executor: # 提交任务到进程池 future_to_function = {executor.submit(my_function): _ for _ in range(max_workers)} # 等待任务完成 for future in as_completed(future_to_function): try: # 获取返回的结果或异常 result = future.result() except Exception as exc: print(f'任务产生了异常: {exc}') else: print(f'任务结果: {result}') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值