Linux 如何使用 bash 命令创建 CPU 峰值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2925606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a CPU spike with a bash command
提问by User1
I want to create a near 100% load on a Linux machine. It's quad core system and I want all cores going full speed. Ideally, the CPU load would last a designated amount of time and then stop. I'm hoping there's some trick in bash. I'm thinking some sort of infinite loop.
我想在 Linux 机器上创建接近 100% 的负载。它是四核系统,我希望所有内核都全速运行。理想情况下,CPU 负载会持续指定的时间然后停止。我希望 bash 有一些技巧。我在想某种无限循环。
采纳答案by dimba
You can also do
你也可以这样做
dd if=/dev/zero of=/dev/null
To run more of those to put load on more cores, try to fork it:
要运行更多这些以将负载加载到更多内核上,请尝试将其分叉:
fulload() { dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null & }; fulload; read; killall dd
Repeat the command in the curly brackets as many times as the number of threads you want to produce (here 4 threads). Simple enter hit will stop it (just make sure no other dd is running on this user or you kill it too).
重复大括号中的命令,次数与要生成的线程数相同(此处为 4 个线程)。简单的回车命中将停止它(只需确保没有其他 dd 在该用户上运行,否则您也将其杀死)。
回答by Marian
An infinite loop is the idea I also had. A freaky-looking one is:
无限循环是我也有的想法。一个看起来很怪异的是:
while :; do :; done
(:
is the same as true
, does nothing and exits with zero)
(:
与 相同true
,什么都不做并以零退出)
You can call that in a subshell and run in the background. Doing that $num_cores
times should be enough. After sleeping the desired time you can kill them all, you get the PIDs with jobs -p
(hint: xargs
)
您可以在子外壳中调用它并在后台运行。这样做$num_cores
时间应该足够了。睡眠所需的时间,你可以把他们都杀掉之后,你得到的PID jobs -p
(提示:xargs
)
回答by Secko
#!/bin/bash
while [ 1 ]
do
#Your code goes here
done
回答by Jeff Goldstein
:(){ :|:& };:
This fork bomb will cause havoc to the CPU and will likely crash your computer.
这个叉子炸弹会对 CPU 造成严重破坏,并可能使您的计算机崩溃。
回答by Fred
I would split the thing in 2 scripts :
我会将这件事分成 2 个脚本:
infinite_loop.bash :
无限循环.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 :
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
回答by Paused until further notice.
#!/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
回答by L??o???n???g???p??o???k???e???
One core (doesn't invoke external process):
一个核心(不调用外部进程):
while true; do true; done
Two cores:
两个核心:
while true; do /bin/true; done
The latter only makes both of mine go to ~50% though...
后者只会让我的两个都达到约 50%...
This one will make both go to 100%:
这将使两者都达到 100%:
while true; do echo; done
回答by ZyX
This does a trick for me:
这对我有用:
bash -c 'for (( I=100000000000000000000 ; I>=0 ; I++ )) ; do echo $(( I+I*I )) & echo $(( I*I-I )) & echo $(( I-I*I*I )) & echo $(( I+I*I*I )) ; done' &>/dev/null
and it uses nothing except bash.
它除了 bash 什么都不使用。
回答by David
回答by Ishtiaq Ahmed
I went through the Internet to find something like it and found this very handy cpu hammer script.
我通过互联网找到了类似的东西,并找到了这个非常方便的cpu锤子脚本。
#!/bin/sh
# unixfoo.blogspot.com
if [ ]; then
NUM_PROC=
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