限制 某进程 使用最大CPU资源 _ cpulimit _ 参考

简介: This tutorial shows how you can limit the CPU usage of a process with the tool cpulimit on Debian/Ubuntu.

This tutorial shows how you can limit the CPU usage of a process with the tool cpulimit on Debian/Ubuntu. cpulimit is a simple program that attempts to limit the CPU usage of a process (expressed in percentage, not in cpu time). This is useful to control batch jobs, when you don't want them to eat too much CPU. It does not act on the nice value or other scheduling priority stuff, but on the real CPU usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I will run all commands in this tutorial as the root user, so either log in as root directly (Debian) or become root like this (Ubuntu):

sudo su

If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power. In any case, the percentage is the same of what you see when you run top.

cpulimit should run at least with the same user running the controlled process. But it is much better if you run cpulimit as root, in order to have a higher priority and a more precise control.

 

2 Installing cpulimit

cpulimit is available as a package for Debian and Ubuntu, so it can be installed as follows:

aptitude install cpulimit

 

3 Using cpulimit

Take a look at the cpulimit man page to learn how to use it:

man cpulimit

NAME
cpulimit -- limits the CPU usage of a process

SYNOPSIS
cpulimit TARGET [OPTIONS...]

DESCRIPTION
TARGET must be exactly one of these:

-p, --pid=N
pid of the process

-e, --exe=FILE
name of the executable program file

-P, --path=PATH
absolute path name of the executable program file

OPTIONS

-l, --limit=N
percentage of CPU allowed from 0 to 100 (mandatory)

-v, --verbose
show control statistics

-z, --lazy
exit if there is no suitable target process, or if it dies

-h, --help
display this help and exit

EXAMPLES
Assuming  you  have  started "foo --bar" and you find out with top(1) or ps(1) that this process uses all your CPU
time you can either

# cpulimit -e foo -l 50
limits the CPU usage of the process by acting on the executable program file (note: the argument "--bar" is
omitted)

# cpulimit -p 1234 -l 50
limits the CPU usage of the process by acting on its PID, as shown by ps(1)

# cpulimit -P /usr/bin/foo -l 50
same as -e but uses the absolute path name

AUTHOR
This  manpage was written for the Debian project by gregor herrmann <gregor+debian@comodo.priv.at> but may be used
by others.

 

 

Now let's assume we want to limit the process apache2 to 30%. This is how we do it:

cpulimit -e apache2 -l 30

The -e switch takes the name of the executable program file. You can take that name from the output of the top command.

Instead of using the name of the executable program file, we can use the process ID with the -p switch. You can find out the process ID of the apache2 process as follows:

ps aux

or

ps aux | grep apache2

Let's assume the apache2 process ID is 4510; we can then limit that process to 30% CPU usage as follows:

cpulimit -p 4510 -l 30

Instead of using the name of the executable file (-e) of the process ID (-p), we can also pass the absolute path name of the executable program file to cpulimit with the -P switch; the absolute path name of the apache2 executable is /usr/sbin/apache2 so we'd use the following command:

cpulimit -P /usr/sbin/apache2 -l 30

Please note that cpulimit will run in the foreground of your terminal until you terminate it with CTRL+C - terminating it will also remove any CPU limits.

 

4 Links




source : http://www.howtoforge.com/how-to-limit-cpu-usage-of-a-process-with-cpulimit-debian-ubuntu

目录
打赏
0
0
0
0
680
分享
相关文章
schedule:原来还可以这样让进程让出 CPU?
文章主要讲述通过模拟时钟中断和调度事件来优化和测试虚拟机监控器(VMM)的方法,包括流程设计、寄存器状态的保存与恢复、硬件中断处理规范等细节。
schedule:原来还可以这样让进程让出 CPU?
基于进程热点分析与系统资源优化的智能运维实践
智能服务器管理平台提供直观的可视化界面,助力高效操作系统管理。核心功能包括运维监控、智能助手和扩展插件管理,支持系统健康监控、故障诊断等,确保集群稳定运行。首次使用需激活服务并安装管控组件。平台还提供进程热点追踪、性能观测与优化建议,帮助开发人员快速识别和解决性能瓶颈。定期分析和多维度监控可提前预警潜在问题,保障系统长期稳定运行。
55 17
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
203 2
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
如何找出Java进程占用CPU高的元凶
本文记录了一次Java进程CPU占用率过高的问题和排查思路。
|
4月前
|
dllhost.exe进程占用CPU很高怎么解决?
全面解析dllhost.exe进程
273 16
|
5月前
|
CPU资源
【10月更文挑战第2天】CPU资源
133 5
【Azure Cloud Service】在Azure云服务中收集CPU监控指标和IIS进程的DUMP方法
在使用Cloud Service服务时,发现服务的CPU占用很高,在业务请求并不大的情况下,需要直到到底是什么进程占用了大量的CPU资源,已经如何获取IIS进程(w3wp.exe)的DUMP文件?
Linux系统CPU异常占用(minerd 、tplink等挖矿进程)
Linux系统CPU异常占用(minerd 、tplink等挖矿进程)
255 4
Linux系统CPU异常占用(minerd 、tplink等挖矿进程)
在Docker中,如何控制容器占用系统资源(CPU,内存)的份额?
在Docker中,如何控制容器占用系统资源(CPU,内存)的份额?
在Linux中,如何查找系统中占用CPU最高的进程?
在Linux中,如何查找系统中占用CPU最高的进程?

相关实验场景

更多