暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

GBase 8a 使用cpulimit限制进程序cpu占用

VV_刺头王 2024-09-12
186

cpulimit是一个限制进程的CPU使用率的工具(以百分比表示,而不是CPU时间)。其工作原理是为进程预设一个CPU占用率限制,并实时监控进程是否超出此限,若超出则让该进程暂停运行一段时间。它不会更改nice值或其他调度优先级设置,而是更改真实的CPU使用率。此外,它能够动态地、快速地适应整个系统负载。cpulimit的优势是可以控制进程的cpu使用率的上限值,但也有缺点,那就是即使cpu是空闲的,进程也不能完全使用整个cpu资源。

1、安装(root用户):yum install -y cpulimit

2、命令语法

cpulimit [OPTIONS...] TARGET

参数释义:

OPTION:

-l, --limit=N                 percentage of cpu allowed from 0 to 400 (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    可执行程序文件的名称或路径名

注意:

(1)-l后面限制的CPU使用量,要根据实际的核心数量而成倍减少。比如对于进程1234,限制CPU使用量为40%,对于1核的服务器,指令为cpulimit -p 1234 -l 40,而对于2核的服务器,指令为cpulimit -p 1234 -l 20,以此类推,4核服务器则为cpulimit -p 1234 -l 10。

(2)root用户可以限制所有的进程,普通用户只能限制自己有权限管理的进程。

3、三种使用方法示例

(1)根据进程pid限制:进程pid为123456的程序只能使用80%的CPU

1核服务器:cpulimit -p 123456 -l 80

2核服务器:cpulimit -p 123456 -l 40

4核服务器:cpulimit -p 123456 -l 20

8核服务器:cpulimit -p 123456 -l 10

(2)根据进程路径限制:test.sh只能使用80%的CPU

1核服务器:cpulimit -e /opt/test.sh -l 80

2核服务器:cpulimit -e /opt/test.sh -l 40

4核服务器:cpulimit -e /opt/test.sh -l 20

8核服务器:cpulimit -e /opt/test.sh -l 10

(3)根据进程名称限制:gbased 只能使用80%的CPU

1核服务器:cpulimit -l 80 gbased

2核服务器:cpulimit -l 40 gbased

4核服务器:cpulimit -l 20 gbased

8核服务器:cpulimit -l 10 gbased

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论