一个限制进程 CPU 使用率的解决方案

一个限制进程 CPU 使用率的解决方案

一 背景

在最近的一个项目中,需要限制 CPU 使用率。通过查阅各种资料,发现已经有直接可以使用的软件可以使用,这个软件就是cpulimit,这个软件使用非常简单。但是,结合实际使用场景,被限制的进程不能后台运行,一旦后台运行,进程即会立刻退出,而且该进程运行一段时间后会产生子进程及相关进程。针对这种情况,经过思考,通过以下解决方案解决该问题。

二 解决步骤

2.1 安装cpulimit

[root@gysl-dev ~]# yum -y install epel-release
[root@gysl-dev ~]# yum -y install cpulimit

2.2 执行脚本

[root@gysl-dev ~]# sh cpulimit.sh

cpulimit.sh脚本内容:

#!/bin/bash
while true;
    do
        sleep 30;
        pgrep palrun>&/dev/null;
        if [ $? -eq 0 ]; then
            for pid in `pgrep palrun`;
                do
                    cpulimit -l 70 -p $pid &
                done;
        break;
        fi;
        done &

将以上脚本加入到需要限制 CPU 使用率的进行启动脚本的最前面,对该脚本的解释。

由于需要限制 CPU 使用率的进程不能在后台运行,所以把限制脚本加入到启动脚本的最前面,并切换到后台运行,sleep 30秒,待需要限制的进程启动并创建子进程后对其进行限制。

三 总结

3.1 官方帮助信息

[root@gysl-dev ~]# cpulimit --help
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)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

3.2 cpulimit命令使用实践

[root@gysl-dev ~]# cpulimit -l 70 -v -p 6258
1 cpu detected
Process 6258 found
Priority changed to -10
Members in the process group owned by 6258: 1

%CPU    work quantum    sleep quantum   active rate
70.09%   73424 us        26575 us       73.42%
69.86%   70778 us        29221 us       70.78%
69.94%   71703 us        28296 us       71.70%
69.77%   70495 us        29504 us       70.50%
69.91%   74194 us        25805 us       74.19%
69.49%   69281 us        30718 us       69.28%
69.78%   72668 us        27331 us       72.67%
70.35%   70634 us        29365 us       70.63%
69.66%   72786 us        27213 us       72.79%
70.27%   69679 us        30320 us       69.68%
69.56%   72325 us        27674 us       72.33%
70.40%   71926 us        28073 us       71.93%
69.43%   71330 us        28669 us       71.33%
69.50%   72184 us        27815 us       72.18%
70.16%   69835 us        30164 us       69.84%
69.37%   74080 us        25919 us       74.08%
69.84%   69417 us        30582 us       69.42%
69.95%   71415 us        28584 us       71.42%
70.81%   71334 us        28665 us       71.33%

原文地址:https://blog.51cto.com/3842834/2364081

时间: 2024-08-02 04:12:52

一个限制进程 CPU 使用率的解决方案的相关文章

老男孩教育每日一题-2017-04-17:使用Shell或Python写一个脚本,CPU使用率超过80%或硬盘超过85%邮件报警

老男孩教育每日一题-2017-04-17: 使用Shell或Python写一个脚本,CPU使用率超过80%或硬盘超过85%邮件报警. 今天是老男孩教育每日一题陪伴大家的第29天.

【研究任务】精准控制进程cpu使用率

一.cpu的使用率计算 cpu的使用率的获取可以通过top命令或者/proc/<pid>/stat文件 以下是计算cpu使用率有关的参数: pid=17184 进程号 utime=45010 该任务在用户态运行的时间,单位为jiffies stime=4050 该任务在核心态运行的时间,单位为jiffies cutime=0 所有已死线程在用户态运行的时间,单位为jiffies cstime=0 所有已死在核心态运行的时间,单位为jiffies #参考python包psutil源码 def c

Java进程CPU使用率高排查

近期java应用,CPU使用率一直很高,经常达到100%,通过以下步骤完美解决,分享一下. 1.jps 获取Java进程的PID. 2.jstack pid >> java.txt 导出CPU占用高进程的线程栈. 3.top -H -p PID 查看对应进程的哪个线程占用CPU过高. 4.echo "obase=16; PID" | bc 将线程的PID转换为16进制. 5.在第二步导出的Java.txt中查找转换成为16进制的线程PID.找到对应的线程栈. 6.分析负载高

Windows下用C语言获取进程cpu使用率,内存使用,IO情况

#ifndef PROCESS_STAT_H #define PROCESS_STAT_H #ifdef __cplusplus extern “C” { #endif typedef long long int64_t; typedef unsigned long long uint64_t; /// 获取当前进程的cpu使用率,返回-1失败 int get_cpu_usage(); /// 获取当前进程内存和虚拟内存使用量,返回-1失败,0成功 int get_memory_usage(ui

使用Shell写一个脚本,CPU使用率超过80%或硬盘超过85%邮件报警

最近公司领导要求,要对客户那边写一个自动化的脚本,用来监控客户那边CPU使用率和硬盘空间,并通过邮件来报警.刚好参考老男孩李导最近的案例,测试了下,可以正常使用. CPU监控:top -n 1 查看1次就退出 1 Cpu(s): 0.3%us,  0.3%sy,  0.0%ni, 99.3%id,  0.0%wa, 0.0%hi,  0.0%si,  0.0%st 99.3%id  是未使用的CPU,剩余的都是使用的. 获取使用率 1 top -n 1|awk -F '[, %]+' 'NR==

关于在zabbix监测脚本中使用ps命令监控进程CPU使用率和内存使用率,获得数据为0的情况描述

前提:想自己编写zabbix监测脚本,然后通过配置模板的方式,实现对资源(cpu和内存)使用率高的进程进行监控. 过程描述:zabbix版本为2.21,被监控主机操作系统为CentOS 6.4.脚本中主要命令如下:percent=0; #通过脚本输入参数process=$1; #通过ps aux参数,获取CPU%和MEM%值,使用awk将第四行的MEM%值筛选出来percent=ps aux | grep $process | grep -v grep | head -1 | awk '{pri

SQLSERVER进程CPU使用率100%

解决SQLSERVER2008 CPU使用率99%: 1.dbcc checkdb 重建索引 2.檢查有沒有死鎖 -- sp_lock SELECT request_session_id spid , OBJECT_NAME(resource_associated_entity_id) tableName     FROM sys.dm_tran_locks     WHERE resource_type = 'OBJECT' -- kill spid 解除死锁 kill 58 到这里查看一下C

Ubuntu下Linux限制进程cpu使用率

安装cpulimit 使用源码安装 # cd /tmp # wget 'http://downloads.sourceforge.net/cpulimit/cpulimit-1.1.tar.gz' # tar cpulimit-1.1.tar.gz# cd cpulimit-1.1 # make # cp cpulimit /usr/local/sbin/ # rm -rf cpulimit *Debian / Ubuntu 用户 使用apt-get更方便的安装 $ sudo apt-get u

查看线程linux cpu使用率

Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算 转 http://www.cnblogs.com/lidabo/p/4738113.html目录(?)[-] proc文件系统 proccpuinfo文件 procstat文件 procpidstat文件 procpidtasktidstat文件 系统中有关进程cpu使用率的常用命令 ps 命令 top命令 单核情况下Cpu使用率的计算 基本思想 总的Cpu使用率计算 计算方法 某一进程Cpu使用率的计算 计算方法 实验数据