Change desktop images based on CPU load via a script

On a few recent occasions, I have had to catch and fix runaway CPU-hog zombie processes. Usually, I notice these after a being frustrated for a period of time by slow machine response times. Using the w command in a terminal shows that my CPU load is too high, but I often don’t think to check that first. Wouldn’t it be great to set up an ambient visual alert, to warn of such system issues in a non-intrusive manner? This is easy with a combination of cron, perl, and a desktop picture set to change every minute.

First, set up a folder containing your regular desktop picture (“normal.pct”) and a warning desktop picture (“warn.pct”). Mine is ~/Picture -> Desktop Pictures. Inside that folder, set up a subfolder called “Active,” and set your desktop preferences to rotate images from that folder every minute or so.

Place the following perl script in your Desktop Pictures folder, give it the proper executable privileges with chmod, and call it activate.pl:

#!/usr/bin/perl
chomp(my $basedir = `dirname "$0"`);
chdir $basedir;
my $active_image = 'normal.pct';
my $load_threshold = 1.7;
if(($_ = `w`) && /load averages: +([\d\.]+) +([\d\.]+) +([\d\.]+)/) {
if($2 > $load_threshold) {
$active_image = 'warn.pct';
}
}
unlink("Active/active_a.pct");
unlink("Active/active_b.pct");
link($active_image, "Active/active_a.pct");
link($active_image, "Active/active_b.pct");

Finally, add the following line to your crontab, using crontab -e:
*/2 * * * * /Users/your_username/Pictures/Desktop\ Pictures/activate.pl

If this works correctly, you will have a desktop image that smoothly changes from normal.pct to warn.pct when your load average stays over 1.7 for five minutes or so. In my case, warn.pct is solid red, and heightened CPU usage for an extended period of time shows up as a slow background fade to fire engine red. If you are comfortable with perl, you can modify the if() statement to change images based on other chosen conditions, or show different images depending on situations that demand your attention.