View valid 'top' output on the desktop via GeekTool
Aug 09, '06 07:30:00AM
Contributed by: hysterion
The great and free GeekTool (PowerPC | Intel) can write the contents of any file, and the output of any shell command, right onto the desktop. Unfortunately it doesn't support self-updating commands like top, and the official workaround (break the loop with top -lk and set Refresh to keep restarting it) is rather Heisenbuggy:
- Taking k = 1, the CPU usage line and column stop making sense.
- With k > 1, they become skewed by an overestimated CPU usage for top itself.
- Either way, relaunching top every few seconds soon causes pid's to roll over 30000, so that recent processes must be hunted down the list rather than conveniently appearing on top.
In short, watching top this way perturbs it more than we'd like. In this hint, I describe a different (k = 0) workaround which appears to solve the problem, i.e. display top exactly like Terminal does.
Here's how to do it:
- In the GeekTool Preference Pane, make a new entry top.log of type File, with Path set to /var/tmp/top.log. This will make GeekTool always display the last 50 lines of this file -- internally it runs tail -n50 -F on it.
- Make another new entry of type Shell, with Command: top.py, Size: 0 by 0, Refresh: 10. Actually, our script will run only once, but for some reason GeekTool gets far more CPU-intensive if the Refresh field is empty or zero.
- Put the following script, top.py, in ~/Library/Application Support/GeekTool Scripts. Be sure to make it executable by running chmod a+x on it, so it will execute.
#!/usr/bin/env python
import os, time
os.system("top -l0 -n45 -s3 -S >> /var/tmp/top.log &")
while True:
open("/var/tmp/top.log","w").close()
time.sleep(60)
Here the os.system line spawns one top process, which forever (-l0) keeps sending 45 lines, every three seconds, to our top.log. The while loop then tames the log's growth by emptying it once a minute. Fewer than 45 lines works fine, but more seem to cause a jerky display.
- Now click Enable GeekTool, select the top.log entry, and customize the window to taste, moving it up so that the first (50 - 45 = 5) lines hide above the Mac menu bar. Close the Pref Pane, and compare the result with top -s3 running in Terminal.
In conclusion, let's hope that new releases of GeekTool will obsolete this hint, and let us also put things like fseventer on the desktop!
Comments (15)
Mac OS X Hints
http://hints.macworld.com/article.php?story=20060806133200865