How to create a CPU spike with bash command on Linux?

If you have been programming, then you might have observed certain cases and scenarios where the program gets stuck or the process is running in an infinite loop which in turn puts pressure on the core of that thread which is handling that process.

There are many such cases where this is quite a possibility.

We usually make use of different techniques to avoid such cases, like handling them in code logic itself or using third party tools to deal with them.

Linux also provides us with a command that we can use to keep track of the different parameters of the CPU and memory usages. The command is top. Just type the following command in the terminal and you will be greeted with a table shape output that will be constantly changing.


Consider the example shown below −

Processes: 480 total, 2 running, 478 sleeping, 1914 threads
Load Avg: 2.33, 3.43, 3.41 CPU usage: 20.68% user, 10.90% sys, 68.40% idle
SharedLibs: 263M resident, 53M data, 254M linkedit.
MemRegions: 179160 total, 1887M resident, 55M private, 494M shared.
PhysMem: 8108M used (1722M wired), 82M unused.
VM: 3412G vsize, 2318M framework vsize, 5752706(0) swapins, 6194358(0)
swapouts. Networks: packets: 22871569/28G in, 5756073/1771M out.
Disks: 6360234/144G read, 5499350/85G written.

6720 Terminal 51.9 02:49.30 8
130 WindowServer 34.0 07:17:41 12
0 kernel_task 11.8 03:23:39 178/4
24506 top 8.3 00:11.75 1/1

The output might seem a bit complex at first, but the only column that we need to consider is the %CPU only. It denotes the percentage of the CPU that the current task is consuming. Notice that it is rarely going above 20% in the process that I was running.

Now, in order to create a spike in the cpu we have different ways that we can use.

The first and the most common way is to make use of the stress command. The stress command is used along with the number of cpu cores and / or the timeout for which you want the cores to max out.

If you don’t have stress installed you can install it with the commands shown below −

For Ubuntu

sudo apt-get install stress

For Mac

brew install stress


stress --cpu n


stress --cpu n --timeout time

You can use either of these commands shown above.

Let’s use the first command where we will ask the stress command to max out two cores of our machine.


Consider the command shown below −

immukul@192 ~ % stress --cpu 2
stress: info: [25675] dispatching hogs: 2 cpu, 0 io, 0 vm, 0 hdd


25676  stress  98.5 00:36.23 1/1
25677  stress  98.4 00:36.42 1/1

Another way to achieve this without using a third party package is to make use of the simple linux code shown below −

for i in 1 2 3 ; do while : ; do : ; done & done

The above one-liner code will result in maxing out three cores of your operating system.


immukul@192 Downloads % for i in 1 2 3 ; do while : ; do : ; done & done
[2] 25691
[3] 25692
[4] 25693


25693  zsh     81.7  00:57.12 1/1
25691  zsh     81.4  00:57.00 1/1
25692  zsh     78.6  00:57.00 1/1

Updated on: 29-Jul-2021


Kickstart Your Career

Get certified by completing the course

Get Started