[不花钱站长]:从免费域名开始[0元] Oracle永久免费VPS[0元] VPS环境搭建免费脚本[0元] 秒变大盘挂载免费网盘[0元] 小白免费采集器1天500W[0元] CF免费DNS+CDN服务[0元]
[零基础网赚]:撸本站免费源码[0元] 网站强引免费蜘蛛法[0元] 图片视频文件免费存储[0元] 外链轻松发布免费友链[0元] 入坑免费教程学习源代码[0元] 网上百种+赚钱联盟推荐[?元]

↑↑VPS推荐网 WWW.VPS.COM.IN 稳定 便宜 免备案↑↑


Linux VPS 主机使用 CPULimit 来限制 Linux 进程的 CPU 使用率

全球[VPS测评]来源:VPS推荐网点击:1047
【性价之王】【线路之王】【价格之王】【配置之王】
【免费之王】【香港首推】【梯子之王】【独服之王】
Linux VPS 主机使用 CPULimit 来限制 Linux 进程的 CPU 使用率

搬瓦工 VPS 都会限制 CPU 使用率,如果超出限制,VPS主机会被暂停。可以使用 CPULimit 每个进程的 CPU 使用率进行限制,可以避免搬瓦工 VPS 的过程中因超出 CPU 限制而被暂停。CPULimit适用于所有的Linux VPS主机,因为搬瓦工 VPS 比较典型,以搬瓦工 VPS 为例。

内容 1 CPULimit 简介 2 CPULimit 安装 3 CPULimit 使用教程 4 CPULimit 高级用法 4.1 相关文章

CPULimit 简介

CPUlimit 是一个限制进程的 CPU 使用率的工具(以百分比表示,而不是以 CPU 时间表示)。 当不希望批处理作业占用太多 CPU 时,控制批处理作业很有用。 目标是防止进程运行超过指定的时间比率。它不会更改 nice 值或其他调度优先级设置,而是更改真实的 CPU 使用率,而且能够动态且快速地适应整个系统负载。 使用的 CPU 数量的控制是通过向进程发送 SIGSTOP 和 SIGCONT POSIX 信号来完成的。 指定进程的所有子进程和线程将共享相同百分比的 CPU。

CPULimit 安装

在 Debian 或 Ubuntu 中,使用 apt 命令安装:

sudo apt-get install cpulimit

在 CentOS、RHEL 或 Fedora 中,启用 EPEL后,再以 yum 安装:

sudo yum install cpulimit

或者直接:

sudo yum install epel-release cpulimit

CPULimit 使用教程

帮助命令如下:

root@hk:~# cpulimit -hCPUlimit version 2.4Usage: cpulimit TARGET [OPTIONS...] [-- PROGRAM] TARGET must be exactly one of these: -p, --pid=N pid of the process -e, --exe=FILE name of the executable program file The -e option only works when cpulimit is run with admin rights. -P, --path=PATH absolute path name of the executable program file OPTIONS -b --background run in background -f --foreground launch target process in foreground and wait for it to exit -c --cpu=N override the detection of CPUs on the machine. -l, --limit=N percentage of cpu allowed from 1 up. Usually 1 - 100, but can be higher on multi-core CPUs (mandatory) -m, --monitor-forks Watch children/forks of the target process -q, --quiet run in quiet mode (only print errors). -k, --kill kill processes going over their limit instead of just throttling them. -r, --restore Restore processes after they have been killed. Works with the -k flag. -s, --signal=SIG Send this signal to the watched process when cpulimit exits. Signal should be specificed as a number or SIGTERM, SIGCONT, SIGSTOP, etc. SIGCONT is the default. -v, --verbose show control statistics -z, --lazy exit if there is no suitable target process, or if it dies -- This is the final CPUlimit option. All following options are for another program we will launch. -h, --help display this help and exit

以 md5sum 进程为例。运行以下命令:

md5sum /dev/urandom

这是一个非常耗 CPU 的进程,这行指令会从 /dev/urandom 读取随机数据,计算其 MD5 检查码,这个指令非常耗费 CPU,而且试算不完的,若要中止此进程,请按下 Ctrl + c。

注意:不要长时间运行此命令,仅教学使用,不然可能你的搬瓦工 VPS 不知不觉就被暂停了。

在这行 md5sum 指令执行之后,我们可以使用 top 指令查看目前系统的状况,此时 md5sum 的 CPU 用量应该会接近 100%,如图所示。

如果想要让这个 md5sum 进程不要吃掉太多的 CPU 资源,可以使用 cpulimit 来限制其 CPU 用量(需先在 top 里查看进程的 PID,然后另开一个终端操作):

cpulimit --pid 11699 --limit 50

执行之后,该进程的 CPU 用量就会被控制在 50% 左右,如下图所示(因为我重新运行了一下,所以 PID 变了,不然是不会变的)。

另外也可以使用进程名称来限制 CPU 使用量:

cpulimit --exe md5sum --limit 50

或者以绝对路径来限制 CPU 使用量,可以避免不同进程有相同进程名的问题:

cpulimit --path /usr/bin/md5sum --limit 50

如果在进程执行前就已经确定要调整 CPU 用量,也可以直接以 cpulimit 来执行进程,例如:

cpulimit --limit 50 -- md5sum /dev/urandom

最后,如果想要停止刚刚已经限制的进程,那么需要通过 top 查找 PID 然后 kill:

kill -9 PID

好像没看到更优雅的结束进程的方式。

CPULimit 高级用法

1、后台运行

cpulimit 在执行时也会占用一个终端机,若想让 cpulimit 在后台运行,可加上 --background 参数:

cpulimit --pid 21203 --limit 50 --background

2、终止 CPU 用量过高的进程

cpulimit 配合 --limit 参数可以限制进程的 CPU 用量上限值,如果进程超过这个上限值,预设会调节 CPU 用量,而如果想要在 CPU 用量过高时直接中止进程,可以加上 –-kill 参数:

cpulimit --pid 21203 --limit 50 --kill

3、自动离开

在默认的状况下,cpulimit 在执行时若没有发现指定的进程(或是指定的进程已经中止了),它还是会持续等待并监控系统的进程,只要有发现符合条件的进程,就会继续进行 CPU 用量的控制。

若想让 cpulimit 在找不到目标进程时自动离开,可以加上 --lazy 参数:

cpulimit --exe md5sum --limit 50 --lazy

4、实用范例

在撰写 bash 脚本时,我们可以先执行一个进程,紧接着从 bash 的 $! 变量读取出前一个执行进程的 PID,这样就可以不需要手动查出进程的 PID 了:

# 运行进程md5sum /dev/urandom &# 限制上一个进程的 CPU 用量cpulimit --pid $! --limit 50

以上就是 cpulimit 这个小工具的一些基础用法和进阶用法,希望对大家有所帮助。


[Bandwagonhost]历史优惠活动内容
  • 搬瓦工 BandwagonHost介绍–性价比高,口碑好,CN2 GIA线路
  • 搬瓦工 BandwagonHost介绍–性价比高,口碑好,CN2 GIA线路
  • BandwagonHost-512M内存年付10刀再次来袭
  • BandwagonHost-512M内存年付10刀再次来袭
  • bandwagonhost-半价再次来袭/G口
  • bandwagonhost-半价再次来袭/G口
  • bandwagonhost/搬瓦工vps-512M内存VPS年付9.9刀(5GSSD版非3G
  • bandwagonhost/搬瓦工vps-512M内存VPS年付9.9刀(5GSSD版非3G
  • bandwagonhost/搬瓦工vps-512M内存VPS年付9.9刀(5GSSD版非3G
  • bandwagonhost/搬瓦工vps-512M内存VPS年付9.9刀(5GSSD版非3G
  • bandwagonhost/搬瓦工vps-终身5折优惠/G口/凤凰城
  • bandwagonhost/搬瓦工vps-凤凰城高速VPS/24小时5折促销
  • bandwagonhost/搬瓦工vps-凤凰城高速VPS/24小时5折促销
  • bandwagonhost-512m内存/5g硬盘/500g流量/年付9.99美元
  • bandwagonhost/搬瓦工vps简单测评-(512m内存/年付9.9刀)!
  • bandwagonhost/搬瓦工vps简单测评-(512m内存/年付9.9刀)!
  • bandwagonhost/搬瓦工vps-5折促销:1g内存/50g硬盘/G口/凤凰城
  • bandwagonhost/搬瓦工vps-5折促销:1g内存/50g硬盘/G口/凤凰城
  • bandwagonhost/搬瓦工vps-5折促销:1g内存/50g硬盘/G口/凤凰城
  • bandwagonhost/搬瓦工vps-5折促销:1g内存/50g硬盘/G口/凤凰城
  • bandwagonhost/搬瓦工VPS-改版(大硬盘) 优惠促销
  • bandwagonhost/搬瓦工-13.99$年/256m内存/10g硬盘/500g流量/
  • bandwagonhost/搬瓦工-年付3.99$/超低价VPS/小内存/SSD硬盘
  • bandwagonhost/搬瓦工vps-128m内存VPS简单测评
  • 神器来袭:bandwagonhost/搬瓦工vps-512m内存VPS年付9.99刀
  • 神器来袭:bandwagonhost/搬瓦工vps-512m内存VPS年付9.99刀
  • 神器来袭:bandwagonhost/搬瓦工vps-512m内存VPS年付9.99刀
  • bandwagonhost/搬瓦工vps-特价64m/96m/128m/512m全部上货
  • bandwagonhost/搬瓦工VPS-7美元/4g内存/30gSSD/2T流量
  • bandwagonhost-4G内存款VPS简单测评
  • bandwagonhost/搬瓦工VPS-7美元/4g内存/30gSSD/2T流量
  • bandwagonhost-4G内存款VPS简单测评
  • 全面补货(5.25):bandwagonhost/搬瓦工vps-128M至4g内存VPS-低
  • bandwagonhost-新增洛杉矶机房,VPS年付3.99美元,4机房
  • bandwagonhost-512M内存洛杉矶机房VPS测试
  • 干货:bandwagonhost/搬瓦工VPS-$18.99年/1g内存/20gSSD/2T
  • #消息→bandwagonhost,支付宝/信用卡/PayPal付款(VPS年付4美元,起步
  • 推荐→bandwagonhost-最新特价版VPS(2015/8)/资源加大/支付宝
  • bandwagonhost-全新机房/FMT/$19.99年付/512M内存/10gSSD/
  • bandwagonhost-费利蒙VPS简单测试,刚上架的新母鸡
  • bandwagonhost-补货/512m内存VPS年付12美元/支付宝
  • bandwagonhost-补货/512m内存VPS年付12美元/支付宝
  • bandwagonhost 最新中国直连VPS简单测评
  • bandwagonhost-全新中国直连路由/VPS/512M内存年付20美元/支付宝付款
  • 好评→bandwagonhost-便宜VPS,稳定VPS,8个机房,支持支付宝
  • #搬瓦工VPS# bandwagonhost 新增洛杉矶MC机房/纽约机房+测评
  • #搬瓦工VPS# bandwagonhost-第8个机房正式上线
  • #重磅消息# bandwagonhost(搬瓦工)上线KVM虚拟VPS/亚洲优化VPS
  • 搬瓦工KVM VPS 测评/bandwagonhost KVM VPS 测评/洛杉矶/亚洲优
  • bandwagonhost kvm vps 测评→Fremont(费利蒙)机房
  • 推荐→搬瓦工/bandwagonhost→KVM VPS 512M内存,年付19.99美元+
  • bandwagonhost搬瓦工VPS→介绍+优惠码+测评+新手使用教程
  • 搬瓦工VPS换IP教程 bandwagonhost换IP
  • 搬瓦工官网(bandwagonhost官网是哪个)
  • 介绍→bandwagon , banwagonhost , bandwagonhost vp
  • 教程→搬瓦工VPS换机房 给bandwagonhost切换机房
  • 搬瓦工VPS(bandwagonhost)→cn2 gia涨价 优惠码 以及一些变动
  • 美国云服务器-美国VPS推荐之“顶级”商家“搬瓦工-bandwagonhost”
  • 搬瓦工香港vps怎么样?简单测评下bandwagonhost香港VPS(HK85 Equin
  • 搬瓦工香港vps怎么样?简单测评下bandwagonhost香港VPS(HK85 Equin

  • 猜你可能想看的VPS


    转载请注明原文地址:http://140.238.13.167:12355/read-227897.html

    下一篇       上一篇