Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Change desktop images based on CPU load via a script UNIX
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.
    •    
  • Currently 2.50 / 5
  You rated: 3 / 5 (6 votes cast)
 
[34,321 views]  

Change desktop images based on CPU load via a script | 17 comments | Create New Account
Click here to return to the 'Change desktop images based on CPU load via a script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Change desktop images based on CPU load via a script
Authored by: mdzorn on Sep 21, '04 11:50:25AM

The Apple ActivityMonitor (found under Utilities) can be run in the dock to display the load continuously without adding much to the system load. The Apple-1 shortcut gives you fast access to all the processes running on your system to quickly identify a runaway culprit.



[ Reply to This | # ]
Change desktop images based on CPU load via a script
Authored by: atverd on Sep 21, '04 12:10:13PM

There are few dock-based applications to monitor your cpu load, there are few desktop applications to do the same and there are few menu-line based applications. I'd recommend MenuMeters - it will display nice small load graph in your menu bar and this is a least intrusive method to monitor your system. It can do a lot more actually.



[ Reply to This | # ]
Change desktop images based on CPU load via a script
Authored by: eriklager on Sep 21, '04 01:20:36PM

Thanks, that's a good hint. I've never thought of using the desktop picture as an indicator.



[ Reply to This | # ]
Change desktop images based on CPU load via a script
Authored by: mandolux on Sep 21, '04 02:07:39PM

This is awesome! Thank you so much for such a great hint (specially for those who hide the dock all the time).



[ Reply to This | # ]
Change desktop images based on CPU load via a script
Authored by: toonerh on Sep 21, '04 02:50:16PM

As the first replier suggested, run Apple Activity Monitor as a startup program with or without a place in the dock. When it first runs, select either vertical or horizontal floating CPU monitor and tuck in the corner (not under dock!). Finally close the main window that slows down the system to update. It will remember these settings across reboot. Plus on dual CPU machine it displays two parallel little usage displays, one for each CPU.

I do this on all my machines, it can't be beat!

Also to get the main window back, click on the CPU usage graph to bring Activity Monitor to the front, and type Command-1.



[ Reply to This | # ]
floating CPU monitor COLOR
Authored by: gio on Sep 22, '04 07:35:41AM

I agree: Activity monitor floating CPU monitor is the best way to go.

One question: does anyone knows how to change the COLOUR of the indicator?

Under Jaguar it was user-customizable, but with Panther the possibility was inexplicably removed, and you must stick to an awful green. :-(



[ Reply to This | # ]
Change desktop images based on CPU load via a script
Authored by: smorr on Sep 21, '04 05:13:13PM

Run Geektool (http://projects/tynsoe.org) with the shell script:

echo ' CPU Hogs'; ps -crx -o %cpu,command | head -n 6 | tail -n 5

Set to file every 5 seconds or so.

set the font to a small nonproportional (monaco or whatever) , set the window as transparent, as "always on top" and tuck it into a corner.

It will unobstusively gives you a list of your 5 top hogs --- so you don't have to hunt for them. -- but it doesn't waste space in the dock or toolbar

Incidently Memory Hogs:

echo ' Memory Hogs'; ps -cmx -o %mem,command | head -n 6 | tail -n 5



[ Reply to This | # ]
Geektool
Authored by: ssevenup on Sep 21, '04 07:09:08PM

Nice!!

---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.



[ Reply to This | # ]
Change desktop images based on CPU load via a script
Authored by: Juneappal on Sep 23, '04 09:47:05AM

how do I put the echo command in my bashrc - I can't get all the extra 's to escape properly.



[ Reply to This | # ]
Change desktop images based on CPU load via a script
Authored by: mhorn on Sep 21, '04 07:17:37PM
Good idea. I personally use LoadInDock. It hasn't been updated for quite a while, but it still works in 10.3 and supports dual processors quite well.

[ Reply to This | # ]
Change desktop images based on CPU load via a script
Authored by: keirstwc on Sep 21, '04 09:37:48PM

After reading some of the responses, I am not sure that everyone knows that the Activity monitor will show the CPU utilization in the Dock. No floating windows or 3rd party apps are needed.

Just start Activity monitor, ctrl-click it and select Dock Icon. Then select CPU Usage or CPU history. It will all show in the dock icon.



[ Reply to This | # ]
Use Cee Pee You
Authored by: fuerst on Sep 22, '04 05:11:45AM

After using Acitivity Monitor, Geektool and others I'm now running Cee Pee You from Unsanity.

It will display a small percentage number of the CPU load in the Menu bar. It takes not much place on my 12" Powerbook monitor.

Get it here: http://www.unsanity.net/ceepeeyou-111.sit



[ Reply to This | # ]
Use Cee Pee You
Authored by: balston on Sep 23, '04 04:09:26PM

CeePeeYou ROCKS!!! I've used it since day one and I love it. It comes in VERY handy when you notice your system dragging and then you look at your menu bar and say ... "Hey..." why is my CPU at 90%???

Unsanity makes some good stuff.



[ Reply to This | # ]
Hint followup from submitter
Authored by: migurski on Sep 22, '04 12:46:51PM

I submitted this hint, though foolishly did so without logging in first. Anyway, a few people rightly point out that CPU monitoring is an easy task, and can be done in the dock or desktop or what-have-you. I have a copy of GeekTool running which does just this on my desktop. The broader point of the suggestion was not about CPU usage (I could have chosen unread mail, feeds, or TCP connections as a metric), but about ambient, visual information. It's one thing to have a tiny graph or widget someplace, and quite another to have your entire environment smoothly change hue depending on a trigger. Try it, it's really cool. Like in submarine movies, when the s#!t hits the fan, and the whole movie turns red and ominous. :)



[ Reply to This | # ]
Hint followup from submitter
Authored by: darndog on Sep 28, '04 07:26:14AM
I Love it, modded it a little for my dual screen with left / right images from mandolux.com also made the warn image come up for minute averages over 5.0, load image still uses 5 min averages, unfamiliar with perl hence the lack of a decent if then else structure but it all works fine.
required files l-normal, l-load, l-warn, r-normal, r-load, r-warn, 'Left' and 'Right' folders for the active images.

My desktop (Elaine dual screen clouds from above site), now becomes overcast under load and downright threatening when under stress ;)
The load image works so much better for nightime use that I'm thinking of setting another cron job to switch that to the default after sun down by switching out the activate.pl file.


#!/usr/bin/perl

chomp(my $basedir = `dirname "$0"`);
chdir $basedir;

my $load_threshold = 1.9;
my $warn_threshold = 5.0;

if(($_ = `w`) && /load averages: +([\d\.]+) +([\d\.]+) +([\d\.]+)/) {
  if($2 <= $load_threshold) {
    $active_image_r = 'r-normal.jpg';
    $active_image_l = 'l-normal.jpg';
  }
  if($2 > $load_threshold) {
    $active_image_r = 'r-load.jpg';
    $active_image_l = 'l-load.jpg';
  }
  if($1 > $warn_threshold) {
    $active_image_r = 'r-warn.jpg';
    $active_image_l = 'l-warn.jpg';
  }

}

unlink("Right/active_a.jpg");
unlink("Right/active_b.jpg");
link($active_image_r, "Right/active_a.jpg");
link($active_image_r, "Right/active_b.jpg");
unlink("Left/active_a.jpg");
unlink("Left/active_b.jpg");
link($active_image_l, "Left/active_a.jpg");
link($active_image_l, "Left/active_b.jpg");
next job is to mod this for my laptop and use osascript to change the single desktop image rather than rotating the desktop.

[ Reply to This | # ]
Change desktop images based on CPU load via a script
Authored by: mrdeep on Sep 23, '04 12:00:07AM

Very nice, I normally use the apple "Aqua Blue" default background, I now made a red version (Aqua Red) that gets activated above a certain level, and I have Aqua Graphite activate below a certain level.

Also, as for menu bar solutions for load averages, i use this: http://www.versiontracker.com/dyn/moreinfo/macosx/16402 , there's the added benefit to one click access to uptime.



[ Reply to This | # ]
Change desktop images based on CPU load via a script
Authored by: darndog on Sep 28, '04 10:52:31PM
Another version, I'm calling this one from GeekTool on my PowerBook but cron would work as well, a bash script checks the load and calls osascript to directly change the desktop to one of four images, it does nothing if the pic would not change.

I don't think there is a way of fading between the images without using the desktop prefs 'rotate' option which this version does not use, but in some ways the change is more noticeable and there's no wavy effect that occurs when the desktop fades to the same image, I would expect lower overheads as well seeing as the desktop is not being switched out every minute.

I have four images, lax.jpg (a dimmed pic) for when it's idling, normal.jpg which is my default desktop, load.jpg (overcast) for when it's been working for a while and warn.jpg (red cast) for when it's sweating. the values for these loads are marked up below and will probably need tweaking for your setup. If anyone wants to clean up the code, I'd welcome the feedback, still learning this stuff. dD


#!/bin/bash

W_D=/Users/darndog/Pictures/Desktop # change this to the folder containing your desktop pics

LAX_T="0.5" # set the nothing doing value
LOAD_T="1.7" # set the busy value
WARN_T="7.0" # set the warning value

Ine_LOAD=`uptime | cut -f4- -d: | awk '{ print $1 }'` # get the load average for the last minute
Five_LOAD=`uptime | cut -f4- -d: | awk '{ print $2 }'` # get the load average for the five minutes

# this block sets the file references for the images
LAX_P=$W_D/lax.jpg
NORM_P=$W_D/normal.jpg
LOAD_P=$W_D/load.jpg
WARN_P=$W_D/warn.jpg

# this block does the maths and change the pic only if neccesary.
if [[ $Ine_LOAD > $WARN_T ]]
then
# echo warn # more redundant debug
/usr/bin/osascript <<END
tell application "Finder"
  set pFile to POSIX file "$WARN_P" as string
  set current to desktop picture as string
	if current is not pFile then
		set desktop picture to file pFile
	end if
end tell
END
elif [[ $Five_LOAD > $LOAD_T ]]
then
# echo load # more redundant debug
/usr/bin/osascript <<END
tell application "Finder"
  set pFile to POSIX file "$LOAD_P" as string
  set current to desktop picture as string
	if current is not pFile then
		set desktop picture to file pFile
	end if
end tell
END
elif [[ $Five_LOAD > $LAX_T ]]
then
# echo norm # more redundant debug
/usr/bin/osascript <<END
tell application "Finder"
  set pFile to POSIX file "$NORM_P" as string
  set current to desktop picture as string
	if current is not pFile then
		set desktop picture to file pFile
	end if
end tell
END
else
# echo lax # more redundant dubug
/usr/bin/osascript <<END
tell application "Finder"
  set pFile to POSIX file "$LAX_P" as string
  set current to desktop picture as string
	if current is not pFile then
		set desktop picture to file pFile
	end if
end tell
END
fi


[ Reply to This | # ]