Coder Perfect

How to use a bash command to produce a CPU spike


On a Linux machine, I want to produce a near-100 percent load. It’s a quad-core system, and I’d like all of the cores to run at full speed. The CPU load should, in theory, last for a set amount of time before stopping. I’m hoping that bash has a trick or two up its sleeve. I’m thinking about a never-ending circle.

Asked by User1

Solution #1

For this, I utilize stress; you can tell it how many cores to max out, and it also allows you to stress memory and disk.

Example of a 60-second stress test on two cores

—cpu 2 —timeout 60 stress

Answered by David

Solution #2

You might also

dd if=/dev/zero of=/dev/null

Try forking it to run more of those, putting additional pressure on more cores:

fulload() { dd if=/dev/zero of=/dev/null | 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 the command in the curly brackets as needed to get the desired number of threads (here 4 threads). It can be stopped with a simple enter keystroke (just make sure no other dd is running on this user or you kill it too).

Answered by dimba

Solution #3

This one appears to be easier. Type the following into Terminal and hit Enter.

yes > /dev/null &

One line may not be enough to fully exploit current CPUs; you may need to repeat the command to exhaust all CPU power.

Simply put, to put an end to all of this,

killall yes

The idea came from here, and while it was designed for Mac users, it should work for *nix users as well.

Answered by user1147015

Solution #4

Despite the fact that I’m late to the party, this post is one of the top results for “create load in linux” on Google.

Although the solution result might be used to generate a system load, I prefer to use sha1sum /dev/zero to impose a stress on a single CPU core.

The goal is to compute a hash sum from an unlimited datastream (such as /dev/zero, /dev/urandom, etc.). Until the process is halted, this process will try to max out a CPU core. Multiple commands can be piped together to provide a load for more cores.

Create a two-core load, for example: /dev/zero sha1sum | sha1sum /dev/zero

Answered by Mitms

Solution #5

To load 3 cores for 5 seconds, do the following:

seq 3 | xargs -P0 -n1 timeout 5 yes > /dev/null

The multiple write() system calls result in a significant kernel (sys) load.

If you prefer a cpu load that is mostly userland:

seq 3 | xargs -P0 -n1 timeout 5 md5sum /dev/zero

If you merely want the load to keep going until you press Ctrl-C, do the following:

seq 3 | xargs -P0 -n1 md5sum /dev/zero

Answered by James Scriven

Post is based on