273

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?

8
  • 1
    is it possible to run multiple instances of cat simultaneously? Jun 30, 2012 at 17:16
  • @NateKoppenhaver: Yes, that seems to be possible when wrapping them in screen sessions. But I would prefer a more sophisticated solution if possible. Jun 30, 2012 at 17:18
  • 2
    Heh, I always used cat /dev/random > /dev/null. Guess /dev/zero works too. :-)
    – oKtosiTe
    Jul 1, 2012 at 7:25
  • 9
    @oKtosiTe cat /dev/random has the side effect of depleting the entropy in /dev/random. There are times you need to conserve entropy, I wouldn't have this as my go to CPU hog. Jul 25, 2012 at 21:41
  • 5
    @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
    Jul 22, 2014 at 18:11

17 Answers 17

302

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
11
  • 37
    on ubuntu, you can install with sudo apt-get install stress
    – ben
    Apr 25, 2013 at 17:50
  • 15
    on debian wheezy too.
    – enapupe
    Oct 15, 2013 at 1:42
  • 12
    On Fedora, sudo yum install stress Nov 13, 2014 at 22:15
  • 16
    Arch: sudo pacman -S stress
    – das_j
    Feb 19, 2015 at 19:44
  • 10
    brew install stress on OS X Jul 16, 2015 at 14:15
150

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

12
  • 1
    Thanks, but & causes a command to run in a separate thread or separate core? I am confused.
    – mmdemirbas
    Jul 4, 2012 at 14:23
  • 5
    @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 .
    – jlliagre
    Jul 5, 2012 at 1:54
  • 2
    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.
    – a coder
    Jun 29, 2015 at 19:42
  • 1
    @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.
    – jlliagre
    Jun 29, 2015 at 22:04
  • 3
    +1 for the lc shell function Jun 27, 2018 at 7:07
53

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.

1
  • 2
    imho this should be the accepted answer because it doesn't require any additional packages and it takes care of all cores.
    – lsu_guy
    May 24, 2023 at 5:03
24

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

4
  • 3
    What program do you used to display CPU usage like this? It reminds me of top, but I don't recall the CPU 'charts'.
    – jftuga
    Jul 1, 2012 at 1:13
  • 13
    @jftuga probably htop, top's prettier brother.
    – BoppreH
    Jul 1, 2012 at 3:38
  • 2
    yes its htop. Best real time, colorful interactive process viewer for linux - htop.sourceforge.net Jul 2, 2012 at 17:12
  • 3
    Wasn't paying attention and ran this on a Windows box. Very bad things...
    – Derrick
    Feb 7, 2013 at 17:43
20

Start two

sha1sum /dev/zero &

commands for every core in your system.

To stop

killall sha1sum

or

kill sha1sum
1
  • 1
    Like this one. Short & sweet and didn't need to download anything.
    – MikeMurko
    Mar 24, 2021 at 3:22
11

I've been developing stress-ng, an updated stress tool that can stress a wide range of aspects of a Linux system. For more information, see http://kernel.ubuntu.com/~cking/stress-ng/

Usage is similar to stress

$ stress-ng --cpu 4 --vm 2 --fork 8 --switch 4 --timeout 1m
stress-ng: info:  [32254] dispatching hogs: 4 cpu, 8 fork, 4 switch, 2 vm
stress-ng: info:  [32254] cache allocate: default cache size: 8192K

Install with

sudo apt-get install stress-ng
1
  • 8
    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.
    – DavidPostill
    Sep 6, 2015 at 20:33
7

I usually take the cpuburn suite:

sudo apt-get install cpuburn
for i in {1..4}; do burnK7 & done

Replace 4 with the number of cores / HT-threads you have or want to stress.

Note: This stresses as much chip area as possible at the same time, it's programmed to generate maximum power dissipation. I had to write this post a second time, somehow my machine didn't like it :-(

You could also do cpuburn in sequences:

burnP6 & burnP6 & burnP6 & burnP6 & 
[1] 28520
[2] 28521
[3] 28522
[4] 28523

And when you want to stop them:

killall burnP6

You could also multiply burnP6 & to match the number of CPU cores on your system.

3

You can run that command as many times as you want, and it will take up a different core each time:

$ CORES=1
$ for i in `seq 1 $CORES`; do cat /dev/zero > /dev/null &
> done
[1] 8388
3
  • 1
    Wouldn't that make terminating the processes a bit of a hassle?
    – oKtosiTe
    Jul 1, 2012 at 7:26
  • 1
    killall cat should do it. Jul 1, 2012 at 21:00
  • 1
    Depending on whether you have other cat processes running (I usually do).
    – oKtosiTe
    Jul 2, 2012 at 13:36
2

I combined both +jlliagre and +ecabuk.

#!/bin/bash
lc() {
    nowMs=$(date +%s)
    (
        pids=""
        cpus=${1:-1}
        seconds=${2:-60}
        echo "[$(date)] loading $cpus CPUs for $seconds seconds"
        echo "[$(date)] Expected completion: [$(date --date=@$(expr $nowMs + $seconds))]"
        trap 'for p in $pids; do kill $p; done' 0
        for ((i=0;i<cpus;i++)); do
            sha1sum /dev/zero &
            pids="$pids $!";
        done
        sleep $seconds
    )
    echo "[$(date)] Done"
}

lc $@
2

https://github.com/GaetanoCarlucci/CPULoadGenerator

pretty simple and scientific solution.

Here you can see dynamics example in which 50% load is generated on CPU core 0:

enter image description here

You can run the process on other cores at the same time.

2

Here's the way I use and there's no need to install anything extra.

For example to start with 4 processes,

nproc | xargs seq | xargs -n1 -P4 md5sum /dev/zero

You can change the number of processes by the option "-P" above.

1

pxz is a parallel implementation of xz.

pxz -9e /dev/zero --stdout >/dev/null should do the trick, as this is quite cpu intensive.

If /dev/zero is not fast enough (you notice that pxz gets I/O throttled) you can do pxz -9e /dev/zero --stdout | pxz -9e --stdout >/dev/null

Newer versions of xz have the --threads option which is a substitute for pxz.

1

I wanted to add this to @jlliagre's comment, but I don't have enough reputation. If you're going to use this code on multiple servers and the CPU count will vary, you can use the following command:

for ((i=1; i<=`nproc --all`; i++)); do while : ; do : ; done & done

This will utilize all of the cores on your server regardless of how many you have. The command nproc is part of coreutils so should be on most Linux installations.

1

You can use:

fulload() { 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 dd if=/dev/zero of=/dev/null for your CPU cores.

Hit any key to stop the test!

1

Dockerized solution, using stress. Can be deployed using k8s to cover multiple machines.

https://hub.docker.com/r/jfusterm/stress

0

A simple command line does it too:

x="x" ; while : ; do x=$x$x ; echo -n "." ; done
5
  • 1
    This would be simpler: while : ; do : ; done
    – jlliagre
    Jul 1, 2012 at 6:30
  • @jlliagre Yours wön't go above loadavg 1.
    – ott--
    Jul 1, 2012 at 11:06
  • Your loop isn't primarily loading the CPU but more filling the memory. It will eventually crash with an out of memory error.
    – jlliagre
    Jul 1, 2012 at 12:31
  • @jlliagre Mine fills memory and swap (if present), thus producing a load of 3 before it killed because it runs out of memory.
    – ott--
    Jul 1, 2012 at 14:14
  • 4
    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
    – jlliagre
    Jul 1, 2012 at 14:52
0

Run the program stress specifying all the cores you have on your CPU:

stress -c `nproc`

You can get it on Debian/Ubuntu: sudo apt install stress

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .