How to stress-test a CPU on a Linux server

In some cases, you may want to voluntarily stress-test the CPU of a Linux server to see what happens (monitoring stuff for example).

Method

First, create a file on your hard drive with the following command:

dd if=/dev/urandom of=testfile count=20 bs=1024k

Next, create a shell script with a md5sum loop as below, and named it "stress_cpu":

#!/bin/sh

i=0

while [ 1 ]
do
  md5sum testfile
  i=`expr $i + 1`
  echo "Iteration: $i"
done

Make the script executable :

chmod +x stress_cpu

Then, you just need to execute it and your CPU load will increase instantly.

./stress_cpu

Ctrl-C will end it, as always.