The following bash script hogs CPU resources (and thus drains laptop battery quickly):
#!/bin/bash
# CPU hogger, using 8 threads
hog='dd if=/dev/urandom | bzip2 -9 >> /dev/null'
cmd=$(echo -n ${hog}; printf " | ${hog}%.0s" {1..7}; echo -n '& read; killall dd')
eval ${cmd}
Explaination:
- 'dd if=/dev/urandom | bzip2 -9' reads random data from /dev/urandom, and compresses it using the best (most CPU consuming) bzip2 algorithm option. This is the CPU hog.
- 'cmd' is a string constructed as a bash command that repeats the CPU hog above 8 times.
- By adding the
&
, we send the hoggers to background. We thenread
from stdin and kill all the hoggers. - The last line evaluates the 'cmd' string as a command.
The script took a (strong) hint from and thank the StackOverflow thread: https://stackoverflow.com/questions/2925606/how-to-create-a-cpu-spike-with-a-bash-command.