自动提升CPU使用率到稳定值的脚本

您所在的位置:网站首页 大漠脚本的cpu占用 自动提升CPU使用率到稳定值的脚本

自动提升CPU使用率到稳定值的脚本

2024-07-12 02:34| 来源: 网络整理| 查看: 265

下面是自动提升CPU使用率到指定值的脚本,希望可以帮到您,

cat cpulimit_m.sh  #!/bin/bash

#   # 2021-03-09 # start : ./cpulimit_m.sh  35 &  # stop : ./cpulimit_m.sh  stop

script_name=$0 need_cpu_used_1=$1

while_i() {                 echo  '''                 f=0;                 while true                 do                     daemon_c=`ps -ef|grep cpulimit_m.sh |grep -v grep|wc -l` ;                     if [[ "$daemon_c" == 0 ]];then                          break                     fi                     let f++ ;                  done ''' | /bin/sh & }

start() { while true do   need_cpu_used_2=$[${need_cpu_used_1} - 0]   cpu_num=`cat /proc/cpuinfo |grep 'processor'|wc -l`   one_cpu_num=$[100 / ${cpu_num}]   cpu_free=`top -bn 1 |grep %Cpu|awk '{print $8}'`   cpu_free=${cpu_free%.*}   cpu_used=$[100 - $cpu_free + 1 ]   nd_cpu_use=$[ ${need_cpu_used_2} - ${cpu_used}]   nd_cpu_sh=$[${nd_cpu_use} / ${one_cpu_num}]   if [ ${cpu_used} -lt ${need_cpu_used_2} ];then

        for i in `seq ${nd_cpu_sh}`         do                 while_i                 pid_array[$i]=$!;         done         for i in "${pid_array[@]}";         do                 echo 'kill ' $i ';'; > /dev/null          done   elif [  ${cpu_used} -ge ${need_cpu_used_2} ];then

        nd_cpu_use=$[${cpu_used} - ${need_cpu_used_2}]         nd_cpu_sh=$[${nd_cpu_use} / ${one_cpu_num}]         if [ ${nd_cpu_sh} != 0 ];then                 kill -9 `ps -ef|grep /bin/sh$ |head -${nd_cpu_sh} |awk '{print $2}'` > /dev/null 2>&1         fi   fi   sleep 60 done }

stop() { ps -ef|grep "$script_name" |grep -v grep |awk '{print $2}'|xargs kill -9  > /dev/null 2>&1

}

main() { if [ $need_cpu_used_1 == 'stop' ];then         stop

else         while true         do                 start          done fi }

if [ $# = 2 -o $# = 1 ];then         main else         echo 'Usage: to start  or  to clear env !' fi 帮忙点点赞,感谢支持!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3