我想在Linux机器上创建一个接近100%的负载。这是四核系统,我要所有核都全速运转。理想情况下,CPU负载将持续一段指定的时间,然后停止。我希望bash里有什么妙招。我在想某种无限循环。
当前回答
cat /dev/urandom > /dev/null
其他回答
:(){ :|:& };:
这个fork炸弹将对CPU造成严重破坏,并可能使您的计算机崩溃。
Dimba的dd if=/dev/zero of=/dev/null绝对是正确的,但值得一提的是验证cpu使用率最大化到100%。你可以用
ps -axro pcpu | awk '{sum+=$1} END {print sum}'
它要求每个进程的1分钟平均cpu使用量的ps输出,然后将它们与awk相加。虽然它是1分钟的平均值,但ps足够聪明,可以知道一个进程是否只运行了几秒钟,并相应地调整时间窗口。因此,您可以使用此命令立即查看结果。
我会把它分成两个脚本:
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
#!/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
我用压力来做这种事情,你可以告诉它要最大化多少内核。它还允许对内存和磁盘施加压力。
例如,对2个核心施加压力60秒
压力——cpu 2——超时60