Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Monitoring CPU Usage
Just type the following command in the terminal and you will be greeted with a table-shaped output that will be constantly changing −
top
Processes: 480 total, 2 running, 478 sleeping, 1914 threads 14:36:22 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. PID COMMAND %CPU TIME #TH 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. 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 was running.
Method 1 − Using the Stress Command
The first and most common way to create a CPU spike 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
Syntax
stress --cpu n
Or with a timeout −
stress --cpu n --timeout time
Example − Stressing 2 CPU Cores
Let's use the stress command to max out two cores of our machine −
stress --cpu 2
stress: info: [25675] dispatching hogs: 2 cpu, 0 io, 0 vm, 0 hdd PID COMMAND %CPU TIME #TH 25676 stress 98.5 00:36.23 1/1 25677 stress 98.4 00:36.42 1/1
Method 2 − Using Bash Loops
Another way to achieve this without using a third party package is to make use of a simple bash one-liner. The following command creates infinite loops that will consume CPU resources −
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. Each loop runs in the background (&) and creates a separate process.
Example Output
for i in 1 2 3 ; do while : ; do : ; done & done
[2] 25691 [3] 25692 [4] 25693 PID COMMAND %CPU TIME #TH 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
Stopping CPU Spike
To stop the CPU spike created by the bash loops, you can kill the processes using −
killall -9 stress
Or find the process IDs and kill them individually −
kill -9 25691 25692 25693
Conclusion
Creating CPU spikes in Linux can be achieved using either the stress command or simple bash loops. The stress command provides more control and safety features, while bash loops offer a quick solution without additional software installation. Both methods are useful for testing system performance and monitoring tools.
