模拟消耗CPU之shell脚本
#!/bin/sh
filename killcpu.sh
if [ $# != 1 ] ; then
echo “USAGE: $0 ”
exit 1;
fi
for i in
seq $1
do
echo -ne ”
i=0;
while true
do
i=i+1;
done” | /bin/sh &
pid_array[
i
]
=
i]=
i
]
=
! ;
done
for i in “${pid_array[@]}”; do
echo ‘kill ’ $i ‘;’;
done
自定义监控消耗百分比#!/bin/bash
now=$(top -bn2 | grep ‘Cpu(s)’ | awk ‘{ print $2 }’ | awk ‘NR
2’ | cut -c 1-4 | awk -F”.” ‘{ pr
int $1 }’)
max=40
min=30
if [ $now -gt $max ];then
ps -ef | grep ‘/bin/sh’ | grep -v grep | grep 99 | awk ‘{ print $2 }’ | awk ‘NR
1’ | xargs kill
-9
elif [ $now -lt $min ];then
sh /opt/usecpu.sh 1
else
exit 0
fi