cpu利用率控制脚本:/data/nlu/cpu_tools/test_image_cpu.py
import os
import threading
import multiprocessing
import argparse
def dead_circle(n):
i = n
while True:
i += 1
n = i * (i + 1)
m = n * (n + 1)
l = m * n + i
if i == 5000000:
i = 0
parser = argparse.ArgumentParser()
parser.add_argument(
"--cores",
type=int,
default=5,
help="core nums")
flags, unparsed = parser.parse_known_args()
for i in range(flags.cores):
# t = threading.Thread(target=dead_circle,args=(i,))
# t.start()
p = multiprocessing.Process(target=dead_circle,args=(i,))
p.start()
启动脚本 /data/nlu/cpu_tools/start.sh
#! /bin/sh
num=10 # python脚本中默认跑5个核,32核的机器cpu利用率大概能到15%,可根据自己机器的核数设定数值来调整cpu利用率
cd /data/nlu/cpu_tools
nohup python test_image_cpu.py --cores ${num} & 2>&1
停止脚本 /data/nlu/cpu_tools/stop.sh
#! /bin/sh
ps -ef | grep test_image_cpu | grep -v grep | awk '{print $2}' | xargs kill -9
定时任务提高cpu利用率
cpu_idle=`top -b -n 1 | grep Cpu | awk '{print int($8)}'`
current_time=`date +%H`
num=10
process_num=`ps -ef | grep test_image_cpu | grep -v grep | wc -l`
if [ ${current_time} -gt 15 -a ${current_time} -lt 20 -a ${cpu_idle} -gt 80 ]; then
if [ ${process_num} -eq 0 ]; then
cd /home/home/nlu
nohup python test_image_cpu.py --cores ${num} & 2>&1
fi
else
if [ ${process_num} -gt 0 ]; then
ps -ef | grep test_image_cpu | grep -v grep | awk '{print $2}' | xargs kill -9
fi
fi
参考:
1.性能监控:shell脚本获取cpu/内存/IO数据