If you’re needing to create some sort of CPU stress test script on the fly with minimal resources on a Linux box, a simple md5sum loop comes in handy. Even with ‘watered-down’ versions of a Linux filesystem (ie. busybox) generally have at least the capabilities of running a loop and have the md5sum tool compiled and ready to use. A quick rundown on how to set one up is outlined below.
First, create a relatively large file (as large as you can make one without being overkill):
dd if=/dev/urandom of=testfile count=20 bs=1024k
Second, create a script (I named it stress_cpu, but you can call it what you want) with the following contents:
#!/bin/sh i=0 while [ 1 ] do md5sum testfile i=`expr $i + 1` echo "Iteration: $i" done
Save the file and then give it executable permissions:
chmod +x stress_cpu
Finally, run it:
You may notice when running this script that the initial md5sum test takes about 10 seconds longer than any subsequent tests. This is due to the way md5sum works and how things are written to cache for faster access. If you don’t want anything to be cached for good measure, and you have /proc mounted with the /proc/sys/vm/drop_caches file, then you can use the command
sync; echo 1 > /proc/sys/vm/drop_caches immediately following the md5sum command in the script. See the
proc man page for more information.