How can I produce high CPU load on a Linux server?
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.
Solution 5
Start two
sha1sum /dev/zero &
commands for every core in your system.
To stop
killall sha1sum
or
kill sha1sum
Related videos on Youtube
Oliver Salzburg
Updated on September 18, 2022Comments
-
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:Is there a better method of testing/maxing-out system resources under load?
-
Nate Koppenhaver almost 12 yearsis it possible to run multiple instances of
cat
simultaneously? -
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 yearsHeh, I always used
cat /dev/random > /dev/null
. Guess/dev/zero
works too. :-) -
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 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 over 8 yearsI've been using
matho-primes all >/dev/null &
but it's undoubtedly not the BEST answer for everyone.
-
-
jftuga almost 12 yearsWhat program do you used to display CPU usage like this? It reminds me of top, but I don't recall the CPU 'charts'.
-
BoppreH almost 12 years@jftuga probably htop, top's prettier brother.
-
jlliagre almost 12 yearsThis would be simpler:
while : ; do : ; done
-
吴环宇 almost 12 yearsWouldn't that make terminating the processes a bit of a hassle?
-
ott-- almost 12 years@jlliagre Yours wön't go above loadavg 1.
-
jlliagre almost 12 yearsYour loop isn't primarily loading the CPU but more filling the memory. It will eventually crash with an out of memory error.
-
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 almost 12 yearsThat'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 almost 12 years
killall cat
should do it. -
吴环宇 almost 12 yearsDepending on whether you have other
cat
processes running (I usually do). -
Pushpak Dagade almost 12 yearsyes its htop. Best real time, colorful interactive process viewer for linux - htop.sourceforge.net
-
mmdemirbas almost 12 yearsThanks, but
&
causes a command to run in a separate thread or separate core? I am confused. -
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 over 11 yearsWasn't paying attention and ran this on a Windows box. Very bad things...
-
ben about 11 yearson ubuntu, you can install with
sudo apt-get install stress
-
enapupe over 10 yearson debian wheezy too.
-
Pedro Melo over 9 yearsOn Fedora,
sudo yum install stress
-
das_j over 9 yearsArch:
sudo pacman -S stress
-
Redi almost 9 yearsAs 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 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 almost 9 years
brew install stress
on OS X -
DavidPostill almost 9 yearsPlease 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 almost 7 yearsor simply end them with
fg
ctrl+c, 4 times. -
Akira Yamamoto about 6 years+1 for the lc shell function
-
markdsievers over 5 yearsFrom 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 almost 5 yearsOn Amazon Linux,
sudo amazon-linux-extras install epel -y && sudo yum install stress -y
(ref) -
Marinaio almost 5 yearsI used this in a script for testing!
-
Shadi over 4 yearsThe link to the stress package is dead
-
Amber.G about 4 yearsThanks 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 about 4 years@Amber.G You can run
htop
as the OP ormpstat
to see per CPU load. -
oneworld about 4 yearsThis one worked on Amazon linux EC2 --- sudo yum install dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm and then sudo yum install stress -y
-
Jack Kinsella almost 4 yearsSuccinctly, if no other BG jobs are running -
kill $(jobs -p)
-
Pavan Kumar almost 4 yearsMy choice is the other one-line solution below which does not require any installation: superuser.com/a/443633/451797
-
Gatonito over 3 yearsLike this one. Short & sweet and didn't need to download anything.