tags:

views:

213

answers:

9

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
+1  A: 
#!/bin/bash
while [ 1 ]
do
        #Your code goes here
done
Secko
Eh, no. Sleeping is not that kind of task that puts lots of laod on the cpu `:-)`
Marian
@Marian Ops! Sorry, of course not! I did that by mistake. :S
Secko
+3  A: 

:(){ :|:& };:

This will cause havoc to the CPU

Jeff Goldstein
Do explain~~~~~
BlueRaja - Danny Pflughoeft
@BlueRaja: Fork bomb: http://www.cyberciti.biz/faq/understanding-bash-fork-bomb/
Moron
It'll help if I make it easier to readfork_bomb() { fork_bomb | fork_bomb forkbomb
Jeff Goldstein
That one fails on the "last a designated amount of time and then stop" criterion ;)
Marian
+1 I'd forgotten about that! Thanks for reminding me.
Secko
looks like a bunch of smiley faces.
Wallacoloo
+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
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
+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
+2  A: 

You can also do

dd if=/dev/zero of/dev/null
dimba
dd deals more with I/O than with CPU usage
Fred
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
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
+1  A: 

I use stress for this kind of thing, you can tell it how many cores to max out.. it allows for stressing memory and disk as well.

David