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


当前回答

我用过bc(二进制计算器),用一大堆小数求圆周率。

$ for ((i=0;i<$NUMCPU;i++));do
    echo 'scale=100000;pi=4*a(1);0' | bc -l &
    done ;\
    sleep 4; \
    killall bc

NUMCPU (Linux下):

$ NUMCPU=$(grep $'^processor\t*:' /proc/cpuinfo |wc -l)

这个方法很强大,但似乎对系统友好,因为我从未使用过这个方法导致系统崩溃。

其他回答

您可以尝试测试加密算法的性能。

openssl speed -multi 4

一个核心(不调用外部进程):

while true; do true; done

两个核心:

while true; do /bin/true; done

后者只会让我的两个都达到50%…

这将使两者都达到100%:

while true; do echo; done
:(){ :|:& };:

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

#!/bin/bash
while [ 1 ]
do
        #Your code goes here
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