Categories
bash cpu linux load

How to create a CPU spike with a bash command

326

I want to create a near 100% load on a Linux machine. It’s quad core system and I want all cores going full speed. Ideally, the CPU load would last a designated amount of time and then stop. I’m hoping there’s some trick in bash. I’m thinking some sort of infinite loop.

1

320

You can also do

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

To run more of those to put load on more cores, try to fork it:

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 many times as the number of threads you want to produce (here 4 threads).
Simple enter hit will stop it (just make sure no other dd is running on this user or you kill it too).

13

  • 40

    dd deals more with I/O than with CPU usage

    – Fred

    May 28, 2010 at 18:06

  • 2

    This actually worked the best for my situation. It also worked in Cygwin. For some reason, the other solutions wouldn’t quite spike the CPU. Adding a count and making four processes in parallel worked perfectly. It spiked the CPU at 100% in top and then back down to zero without any help. Just four lines of code and a “wait”.

    – User1

    May 28, 2010 at 22:30


  • 73

    Reading from /dev/zero and writing to /dev/null is not a very good load generator – you have to run a lot of them to generate significant load. Better to do something like dd if=/dev/urandom | bzip2 -9 >> /dev/null. /dev/urandom requires significantly more effort to generate output, and bzip2 will expend a lot of effort trying to compress it, so the overall CPU usage is a lot higher than “fill a block with zeros, and then throw it away”.

    – twalberg

    Sep 10, 2013 at 15:46

  • 5

    Use jobs -p | xargs kill to only kill the processes you have created.

    – Marian

    Feb 10, 2015 at 15:33

  • 6

    @twalberg, you should make your comment into an answer.

    Jul 28, 2015 at 9:55

320

You can also do

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

To run more of those to put load on more cores, try to fork it:

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 many times as the number of threads you want to produce (here 4 threads).
Simple enter hit will stop it (just make sure no other dd is running on this user or you kill it too).

13

  • 40

    dd deals more with I/O than with CPU usage

    – Fred

    May 28, 2010 at 18:06

  • 2

    This actually worked the best for my situation. It also worked in Cygwin. For some reason, the other solutions wouldn’t quite spike the CPU. Adding a count and making four processes in parallel worked perfectly. It spiked the CPU at 100% in top and then back down to zero without any help. Just four lines of code and a “wait”.

    – User1

    May 28, 2010 at 22:30


  • 73

    Reading from /dev/zero and writing to /dev/null is not a very good load generator – you have to run a lot of them to generate significant load. Better to do something like dd if=/dev/urandom | bzip2 -9 >> /dev/null. /dev/urandom requires significantly more effort to generate output, and bzip2 will expend a lot of effort trying to compress it, so the overall CPU usage is a lot higher than “fill a block with zeros, and then throw it away”.

    – twalberg

    Sep 10, 2013 at 15:46

  • 5

    Use jobs -p | xargs kill to only kill the processes you have created.

    – Marian

    Feb 10, 2015 at 15:33

  • 6

    @twalberg, you should make your comment into an answer.

    Jul 28, 2015 at 9:55

157

I think this one is simpler. Open Terminal and type the following and press Enter.

yes > /dev/null &

To fully utilize modern CPUs, one line is not enough, you may need to repeat the command to exhaust all the CPU power.

To end all of this, simply put

killall yes

The idea was originally found here, although it was intended for Mac users, but this should work for *nix as well.

2

  • 8

    +1 Works like a charm, thank you! Worth adding: this command will max out one hyperthread per cpu core. So a dual core cpu (each core having 2 threads) will get a total load of 25% per yes command (assuming the system was otherwise idle).

    – GitaarLAB

    Jan 13, 2014 at 5:13

  • Just to add to this, Each iteration of this command adds 25 percent load on the CPU (Android) up to 4 iterations and the rest have no effect (even in terms of clock rate).

    Mar 16, 2017 at 11:00