How can I produce high CPU load on a Linux server?

Try stress It's pretty much an equivalent of the Windows consume.exe:

oliver$ ./stress --cpu 3
stress: info: [18472] dispatching hogs: 3 cpu, 0 io, 0 vm, 0 hdd

No need to install any extra package, your good old shell is able to do it alone.

This one-liner will load your four cores1 at 100%:

for i in 1 2 3 4; do while : ; do : ; done & done

How it works is quite simple, it starts four endless loops. Each of them is repeating the null instruction (:). Each loop is able to load a CPU core at 100%.

If you use bash, ksh93 and other shells supporting ranges, (i.e. not dash or older ksh), you can use this non portable syntax:

for i in {1..4}; do ...

Replace 4 with the number of CPUs you'd like to load if different from 4.

Assuming you had no background job already running when you launched one of these loops, you can stop the load generation with that command:

for i in 1 2 3 4; do kill %$i; done

Answering @underscore_d's comment, here is an enhanced version that simplify a lot stopping the load and that also allow specifying a timeout (default 60 seconds.) A Control-C will kill all the runaway loops too. This shell function works at least under bash and ksh.

# Usage: lc [number_of_cpus_to_load [number_of_seconds] ]
lc() {
  (
    pids=""
    cpus=${1:-1}
    seconds=${2:-60}
    echo loading $cpus CPUs for $seconds seconds
    trap 'for p in $pids; do kill $p; done' 0
    for ((i=0;i<cpus;i++)); do while : ; do : ; done & pids="$pids $!"; done
    sleep $seconds
  )
}

1Note that with CPUs supporting more than one thread per core (Hyper-threading), the OS will dispatch the load to all virtual CPUs. In that case, the load behavior is implementation dependent (each thread might be reported as 100% busy or not)..


I made a simple python script which does the same. You can control the number of cpu cores you want to load. The good thing about this is that it won't consume any other resource besides the cpu. (I think mark johnson's idea would consume a lot of I/O resources, which is undesired here.)

from multiprocessing import Pool

def f(x):
    # Put any cpu (only) consuming operation here. I have given 1 below -
    while True:
        x * x

# decide how many cpus you need to load with.
no_of_cpu_to_be_consumed = 3

p = Pool(processes=no_of_cpu_to_be_consumed)
p.map(f, range(no_of_cpu_to_be_consumed))

Just run this script from the terminal $ python temp1.py. You need to kill the script when you are done.

Here, is my cpu consumption output when I load 3 of my cores.

Script temp1.py creates three processes (PIDs - 9377, 9378, 9379) which load 3 of my cores