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

381,292

Solution 1

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

Solution 2

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)..

Solution 3

One alternative way would be

openssl speed -multi $(grep -ci processor /proc/cpuinfo)

or (if nproc is present)

openssl speed -multi $(nproc --all)

OpenSSL is almost always present on nowadays distros, so no extra packages needed.

Solution 4

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

Solution 5

Start two

sha1sum /dev/zero &

commands for every core in your system.

To stop

killall sha1sum

or

kill sha1sum
Share:
381,292

Related videos on Youtube

Oliver Salzburg
Author by

Oliver Salzburg

Updated on September 18, 2022

Comments

  • Oliver Salzburg
    Oliver Salzburg almost 2 years

    I’m currently in the process of debugging a Cacti installation and want to create CPU load to debug my CPU utilization graphs.

    I tried to simply run cat /dev/zero > /dev/null, which works great but only utilizes 1 core:

    enter image description here

    Is there a better method of testing/maxing-out system resources under load?

    Related: How can I produce high CPU load on Windows?

    • Nate Koppenhaver
      Nate Koppenhaver almost 12 years
      is it possible to run multiple instances of cat simultaneously?
    • Oliver Salzburg
      Oliver Salzburg almost 12 years
      @NateKoppenhaver: Yes, that seems to be possible when wrapping them in screen sessions. But I would prefer a more sophisticated solution if possible.
    • 吴环宇
      吴环宇 almost 12 years
      Heh, I always used cat /dev/random > /dev/null. Guess /dev/zero works too. :-)
    • Luc
      Luc almost 10 years
      @oKtosiTe What Rich Homolka said is right, but it's not just that it's a bad thing to do, it's also useless because it'll block almost immediately and stop consuming cpu.
    • Luc
      Luc almost 10 years
      @Stefan Interesting, I didn't know that tool existed. Still, it isn't installed by default on any Debian (based) distribution that I know of.
    • Wildcard
      Wildcard over 8 years
      I've been using matho-primes all >/dev/null & but it's undoubtedly not the BEST answer for everyone.
  • jftuga
    jftuga almost 12 years
    What program do you used to display CPU usage like this? It reminds me of top, but I don't recall the CPU 'charts'.
  • BoppreH
    BoppreH almost 12 years
    @jftuga probably htop, top's prettier brother.
  • jlliagre
    jlliagre almost 12 years
    This would be simpler: while : ; do : ; done
  • 吴环宇
    吴环宇 almost 12 years
    Wouldn't that make terminating the processes a bit of a hassle?
  • ott--
    ott-- almost 12 years
    @jlliagre Yours wön't go above loadavg 1.
  • jlliagre
    jlliagre almost 12 years
    Your loop isn't primarily loading the CPU but more filling the memory. It will eventually crash with an out of memory error.
  • ott--
    ott-- almost 12 years
    @jlliagre Mine fills memory and swap (if present), thus producing a load of 3 before it killed because it runs out of memory.
  • jlliagre
    jlliagre almost 12 years
    That's the problem. You aren't answering the question asked which is how to produce a high CPU load on a server. Your script is quickly making a system unresponsive and then crashes. There are much more reliable ways to get a loadavg of 3. eg: for i in 1 2 3; do while : ; do : ; done & ; done
  • Christian Mann
    Christian Mann almost 12 years
    killall cat should do it.
  • 吴环宇
    吴环宇 almost 12 years
    Depending on whether you have other cat processes running (I usually do).
  • Pushpak Dagade
    Pushpak Dagade almost 12 years
    yes its htop. Best real time, colorful interactive process viewer for linux - htop.sourceforge.net
  • mmdemirbas
    mmdemirbas almost 12 years
    Thanks, but & causes a command to run in a separate thread or separate core? I am confused.
  • jlliagre
    jlliagre almost 12 years
    @mmdemirbas: The ampersand causes the command to run as a separate process. The scheduler is then dispatching all the active processes to all available cores .
  • Derrick
    Derrick over 11 years
    Wasn't paying attention and ran this on a Windows box. Very bad things...
  • ben
    ben about 11 years
    on ubuntu, you can install with sudo apt-get install stress
  • enapupe
    enapupe over 10 years
    on debian wheezy too.
  • Pedro Melo
    Pedro Melo over 9 years
    On Fedora, sudo yum install stress
  • das_j
    das_j over 9 years
    Arch: sudo pacman -S stress
  • Redi
    Redi almost 9 years
    As a reminder you can stop this test by issuing killall bash - just make sure you don't have any other important script running at the time.
  • jlliagre
    jlliagre almost 9 years
    @acoder Thanks for suggesting a way to end the loop. I would however avoid killall bash. Answer edited to add a safer method to end the load generation.
  • Christian Long
    Christian Long almost 9 years
    brew install stress on OS X
  • DavidPostill
    DavidPostill almost 9 years
    Please read How do I recommend software for some tips as to how you should go about recommending software. At the very least you should provide more than just/at least a link, for example some additional information about the software itself, and how it can be used to solve the problem in the question.
  • phil294
    phil294 almost 7 years
    or simply end them with fg ctrl+c, 4 times.
  • Akira Yamamoto
    Akira Yamamoto about 6 years
    +1 for the lc shell function
  • markdsievers
    markdsievers over 5 years
    From source: wget http://people.seas.harvard.edu/~apw/stress/stress-1.0.4.tar.‌​gz && tar -xvf stress-1.0.4.tar.gz && cd stress-1.0.4 && ./configure && make && sudo make install See README.
  • Shadi
    Shadi almost 5 years
    On Amazon Linux, sudo amazon-linux-extras install epel -y && sudo yum install stress -y (ref)
  • Marinaio
    Marinaio almost 5 years
    I used this in a script for testing!
  • Shadi
    Shadi over 4 years
    The link to the stress package is dead
  • Amber.G
    Amber.G about 4 years
    Thanks for the response. I tried 'for i in 1 2 3 4; do while : ; do : ; done & done'. My question is how to verify/check the CPU load accordingly? I used 'top', however, the % load per each thread is not 100%...Please advise.
  • jlliagre
    jlliagre about 4 years
    @Amber.G You can run htop as the OP or mpstat to see per CPU load.
  • oneworld
    oneworld about 4 years
    This one worked on Amazon linux EC2 --- sudo yum install dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.r‌​pm and then sudo yum install stress -y
  • Jack Kinsella
    Jack Kinsella almost 4 years
    Succinctly, if no other BG jobs are running - kill $(jobs -p)
  • Pavan Kumar
    Pavan Kumar almost 4 years
    My choice is the other one-line solution below which does not require any installation: superuser.com/a/443633/451797
  • Gatonito
    Gatonito over 3 years
    Like this one. Short & sweet and didn't need to download anything.