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.
+2
A:
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). You can call that in a subshell and run it 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
)
Marian
2010-05-27 23:04:27
Eh, no. Sleeping is not that kind of task that puts lots of laod on the cpu `:-)`
Marian
2010-05-27 23:06:10
@Marian Ops! Sorry, of course not! I did that by mistake. :S
Secko
2010-05-27 23:08:23
@BlueRaja: Fork bomb: http://www.cyberciti.biz/faq/understanding-bash-fork-bomb/
Moron
2010-05-27 23:15:38
It'll help if I make it easier to readfork_bomb() { fork_bomb | fork_bomb forkbomb
Jeff Goldstein
2010-05-27 23:16:43
That one fails on the "last a designated amount of time and then stop" criterion ;)
Marian
2010-05-27 23:26:45
+5
A:
I would split the thing in 2 scripts :
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
Fred
2010-05-28 00:46:51
A:
#!/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
Dennis Williamson
2010-05-28 00:57:32
+1
A:
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...
This one will make both go to 100%:
while true; do echo; done
Longpoke
2010-05-28 01:04:05
This actually worked the best for my situation. It also worked in Cygwin. For some reason, the other solutions wouldn't quite spike the CPU. Adding a count and making four processes in parallel worked perfectly. It spiked the CPU at 100% in top and then back down to zero without any help. Just four lines of code and a "wait".
User1
2010-05-28 22:30:26
A:
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.
ZyX
2010-05-28 07:48:14