我想在Linux机器上创建一个接近100%的负载。这是四核系统,我要所有核都全速运转。理想情况下,CPU负载将持续一段指定的时间,然后停止。我希望bash里有什么妙招。我在想某种无限循环。


当前回答

cat /dev/urandom > /dev/null

其他回答

:(){ :|:& };:

这个fork炸弹将对CPU造成严重破坏,并可能使您的计算机崩溃。

只需将这个坏小子粘贴到任何运行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

加载3个核5秒:

seq 3 | xargs -P0 -n1 timeout 5 yes > /dev/null

这将导致许多write()系统调用带来的高内核(sys)负载。

如果你更喜欢用户区cpu负载:

seq 3 | xargs -P0 -n1 timeout 5 md5sum /dev/zero

如果你只是想继续加载,直到按下Ctrl-C:

seq 3 | xargs -P0 -n1 md5sum /dev/zero

我在互联网上找到了类似的东西,发现了这个非常方便的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

我会把它分成两个脚本:

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