draft0 - a shared blog by just some people

Menu

Bash script to burn your CPU easily

When making my own CPU cooler or testing some other stuff I sometimes need to create as high of a CPU load as possible. After typing while true; do true; done for the one-too-manyth time I finally decided to create an alias for that. But that didn't feel neat enough. So I made this simple Bash script.


#!/bin/bash

if [ "$#" -ne "1" ]
then
  echo "I need one argument and one argument only: Number of threads to start."
  echo "Hint: You have $(nproc) CPU cores."
  exit 1
fi

if [[ "$1" =~ ^[0-9]+$ ]]
then
  n="$1"
else
  echo "Argument needs to be a number: Number of threads to start."
  echo "Hint: You have $(nproc) CPU cores."
  exit 1
fi

trap "pkill -P $$; exit 0" SIGHUP SIGINT SIGTERM

while [ "$n" -gt 1 ]
do
  while true; do true; done &
  n=$((n-1))
done
echo "Started $1 threads to burn your CPU."
pidof -x "$(basename -- $0)"
while true; do true; done

Download

For a simpler script, you can also just use this three-liner without any arguments to stress all CPU cores:


#!/bin/bash
yeah() { while true; do true; done& }
threads=$(nproc)
for i in $(seq 1 $threads); do yeah; done
Comment via email
Mastodon