我想在Linux机器上创建一个接近100%的负载。这是四核系统,我要所有核都全速运转。理想情况下,CPU负载将持续一段指定的时间,然后停止。我希望bash里有什么妙招。我在想某种无限循环。
当前回答
只需将这个坏小子粘贴到任何运行linux的服务器的SSH或控制台。您可以手动终止进程,但我只是在完成后关闭服务器,这样更快。
编辑:我已经更新了这个脚本,现在有一个定时器功能,这样就不需要杀死进程。
read -p "Please enter the number of minutes for test >" MINTEST && [[ $MINTEST == ?(-)+([0-9]) ]]; NCPU="$(grep -c ^processor /proc/cpuinfo)"; ((endtime=$(date +%s) + ($MINTEST*60))); NCPU=$((NCPU-1)); for ((i=1; i<=$NCPU; i++)); do while (($(date +%s) < $endtime)); do : ; done & done
其他回答
#!/bin/bash
duration=120 # seconds
instances=4 # cpus
endtime=$(($(date +%s) + $duration))
for ((i=0; i<instances; i++))
do
while (($(date +%s) < $endtime)); do :; done &
done
我会把它分成两个脚本:
infinite_loop。bash:
#!/bin/bash
while [ 1 ] ; do
# Force some computation even if it is useless to actually work the CPU
echo $((13**99)) 1>/dev/null 2>&1
done
cpu_spike。bash:
#!/bin/bash
# Either use environment variables for NUM_CPU and DURATION, or define them here
for i in `seq ${NUM_CPU}` : do
# Put an infinite loop on each CPU
infinite_loop.bash &
done
# Wait DURATION seconds then stop the loops and quit
sleep ${DURATION}
killall infinite_loop.bash
增加负载或消耗CPU 100%或X%
sha1sum /dev/zero &
在某些系统上,这将增加X%插槽的负载,在这种情况下,你必须多次运行相同的命令。
然后你可以通过输入命令查看CPU使用情况
top
释放负荷
killall sha1sum
我在互联网上找到了类似的东西,发现了这个非常方便的cpu锤子脚本。
#!/bin/sh
# unixfoo.blogspot.com
if [ $1 ]; then
NUM_PROC=$1
else
NUM_PROC=10
fi
for i in `seq 0 $((NUM_PROC-1))`; do
awk 'BEGIN {for(i=0;i<10000;i++)for(j=0;j<10000;j++);}' &
done
:(){ :|:& };:
这个fork炸弹将对CPU造成严重破坏,并可能使您的计算机崩溃。