Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In this tutorial, we’ll be taking a practical approach to creating CPU spike, which can be useful in certain development and testing scenarios.

First, we’ll see how we can check for CPU usage using a couple of different commands.

Afterward, we’ll cover multiple methods, using a variety of commands and utilities, to create CPU spikes.

2. Determine CPU Usage

On Linux, it’s pretty straightforward to check how busy the CPU is at the given moment. There are multiple tools in our arsenal that we can leverage, such as top and vmstat.

2.1 top

The top interactive utility is available on most Linux distributions:

$ top -i

By default, it will display the usage for the first CPU. We can press 1 to display the usage of all CPUs.

2.2. htop

Additionally, we can also use htop, which is a more refined version of top that makes it easy to interact with the task manager with a simple user interface and colorful output. It’s available on most Linux systems, and we can launch it by simply typing htop:

$ htop -p 0

The -p argument is used to specify the PID of a process. By default, htop without arguments will monitor all the processes. In our case, we want to see the CPU usage, so we discard the individual process monitoring.

2.3. Using Graphical CPU Monitors

Alternatively, we have access to lots of graphical task monitors that provide an interactive user interface. Some of the popular ones include gnome-system-monitor and ksysguard. We can install them from our distribution repository using a package manager.

To learn more about monitoring CPU usage, refer to our article on How to Separate CPU Core Usage for a Process.

3. Creating CPU Spikes

Creating a CPU spike on Linux is very easy due to the availability of various tools and commands. If we were to carry out a benchmark, test, or some profiling, we could use the methods below. Moreover, we can open up a CPU monitor in one of the window clients and execute the command in another for better observability.

Note: Before putting these methods into use, be aware that some of the methods will crash the system. We highly recommend saving any work in progress beforehand.

3.1. Using the dd Command

The dd utility is available on most Unix-like systems. We can copy the contents of /dev/zero to /dev/null using the dd command:

$ dd if=/dev/zero of=/dev/null
  • The if an argument is for specifying the input file — in our case, it’s /dev/zero, which is a special file that contains an indefinite number of null characters
  • The of argument is for specifying the output file — in this case, we’ll save the contents to /dev/null, which is a void that discards the contents written to it

Once we execute the command, we can see that it overloads one of our CPUs.

However, we can write a script that we can execute more than once at the same time to overload the other cores as well:


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

Let’s go ahead and make the script executable:

$ chmod +x ./
$ ./

Once we execute the script, we can observe that it will overload four cores in our system. Once we’re done, we can press <Enter> or <CTRL-C> to kill the process.

3.2. Using the stress Tool

The stress tool is a minimal utility that we can use to stress test our system’s CPU, memory, and I/O. By default, stress isn’t installed on most of the distributions. However, it’s available on most official package repositories.

To install for Ubuntu-based distributions, use apt:

# apt install stress

Use yum on Fedora and RHEL:

# yum install stress

For Arch-based distributions, we can use pacman:

# pacman -Sy stress

Once the installation is complete, we can use stress by specifying the resource to test:

$ stress --cpu 2
stress: info: [19941] dispatching hogs: 2 cpu, 0 io, 0 vm, 0 hdd

In our case, we want to stress the CPU. So, we passed the –cpu argument followed by the number of workers to spawn. Once again, we can press <CTRL-C> to kill the process.

3.3. Using the yes Command

yes is a fun utility that repeatedly prints a string passed as an argument. Certainly, we can use it to overload our CPU. First of all, we need to check if yes is available on our system:

$ yes --version

If it’s not available, we can get it by installing the coreutils package available in our packages repository. Let’s put the command into action:

$ yes "scientia potentia est"

As we can see, it prints the string to our console without stopping. Alternatively, we can also redirect the output to /dev/null:

$ yes > /dev/null

However, if we check our CPU usage, we can see that it only overloads one of our CPUs. Depending on the situation, we might need to overload more cores:

$ yes > /dev/null | yes > /dev/null

We can have more instances piped together to overload more cores. For instance, if we have eight cores, we can have eight instances of the same command to overload them.

3.4. Using the sha1sum Utility

The sha1sum is a computer program that verifies the integrity of files by calculating and verifying SHA1 hashes. It ships with most Linux distributions. To use the utility, we can use the reading from /dev/zero to keep the sha1sum utility running. We can pipe the output of the first instance to the second instance:

$ sha1sum /dev/zero | sha1sum /dev/zero

As usual, we can press <CTRL-C> to kill the processes.

3.5. Using the while Loop

We can use the while loop available in Bash to overload the cores. It can come in handy when none of the above-mentioned tools are available and we need to overload the CPU(s) quickly. Here’s a simple while loop:

$ while :; do :; done

The colon is similar to true, which does nothing and exits with 0. However, it will only overload one of the CPU cores. As a substitute, we’ll modify the command a bit to overload two cores:

$ while true; do echo; done

We should note that it won’t work for more than two cores.

3.6. Launching a Fork Bomb Attack

The fork bomb function launches a denial-of-service attack against the CPU resources. Once we call the function, it constantly calls the fork function to replicate itself indefinitely. As a result, it exhausts the system resources, which leads to resource starvation. To call the fork bomb, simply type in the following unusual command:

$ :(){ :|:& };:

To better understand the structure, let’s format it a bit inside a script:


:() {
  :|: &;


The first colon indicates the fork bomb function. So, we defined a function, and in the function body, we put multiple calls to the function itself. The last colon in the script indicates a call to the fork bomb function.

We should avoid running this command and only use it as a last resort because this command will likely crash our system. However, if we run the command, we can kill it with the kill command once we’re done using it.

3.7. Reading from /dev/urandom

The /dev/urandom file is a special file that generates random content. Let’s test it out:

$ cat /dev/urandom

As we can see, it prints random characters. We can redirect its output to /dev/null and observe the CPU usage:

$ cat /dev/urandom > /dev/null

As we can see, it overloads the CPUs but not exactly to 100%. It might come in handy when we don’t need to load the cores to the max.

4. Conclusion

In this article, we saw how we could monitor our CPU usage using top and htop. Afterward, we looked at different commands, tools, and techniques to overload our CPU cores. By now, we should be comfortable enough to modify the commands to best align with our requirements.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!