关于怎样用shell脚本消耗机器的CPU的问题
首先想到的还是使用死循环消耗CPU资源。搜索网上的资料,总结如下:
该脚本,如果服务器是有多颗CPU,可以选择消耗多少颗CPU的资源;使用方法很简单,参数8表示消耗8颗CPU的资源,运行后,会有一堆 kill 命令,运行这些命令即可kill掉死循环进程。
#!/bin/bash
# Destription: testing cpu usage performance
# Example : sh cpu_usage.sh 12
# Remark : cat /proc/cpuinfo | grep "processor"|wc -l #12==>Get the number of processor
# Date : 2015-1-12
# update : 2015-1-12
endless_loop()
{
echo -ne "i=0;
while true
do
i=i+100;
i=100
done" | /bin/bash &
}
if [ $# != 1 ] ; then
echo "USAGE: $0 <CPUs>"
exit 1;
fi
for i in `seq $1`
do
endless_loop
pid_array[$i]=$! ;
done
for i in "${pid_array[@]}"; do
echo 'kill ' $i ';';
done
运行命令: ./killcpu.sh 8
输出:
kill 20926 ;
kill 20928 ;
kill 20930 ;
kill 20932 ;
kill 20934 ;
kill 20936 ;
kill 20938 ;
kill 20940 ;
用top看资源消耗如下:
top - 00:46:27 up 336 days, 4:20, 24 users, load average: 4.52, 1.35, 0.65
Tasks: 153 total, 10 running, 143 sleeping, 0 stopped, 0 zombie
Cpu(s):98.8% us, 1.2% sy, 0.0% ni, 0.0% id, 0.0% wa, 0.0% hi, 0.0% si
Mem: 5120132k total, 4789128k used, 331004k free, 53176k buffers
Swap: 2096440k total, 333412k used, 1763028k free, 669692k cached
参考:
1、http://zh.wikipedia.org/wiki/Fork%E7%82%B8%E5%BC%B9
2、关键字符“fork炸弹”