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


Click here to return to the 'View valid 'top' output on the desktop via GeekTool' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
View valid 'top' output on the desktop via GeekTool
Authored by: jaysoffian on Aug 09, '06 07:44:53PM
Here is an alternate python script that does what you want:

#!/usr/bin/python
import os, sys
from fcntl import fcntl, F_SETFL
from posix import O_NONBLOCK
from select import select

log = open("/var/tmp/top.log", "w")
top = os.popen("top -l0 -n45 -s3 -S")
top_fd = top.fileno()
fcntl(top_fd, F_SETFL, O_NONBLOCK)
_buf = ""
#clear = os.popen("clear").read()
while 1:
	fds, junk1, junk2 = select([top_fd], [], [], .5)
	if top_fd not in fds: continue
	buf, _buf = _buf, ""
	buf += top.read()
	if buf.endswith("\n"):
		if buf[0] == "\n": buf = buf[1:]
#		sys.stdout.write(clear + buf)
		log.seek(0)
		log.write(buf)
		log.truncate()
		buf = ""
	else:
		_buf = buf

This is slightly complicated because 45 lines of output is just *a byte or two* over 4k which is the popen buffer size, so the reads were getting output split across each emission from top. If you use 44 lines then the program can simplified a bit, but hey, here's something that works with any line count. :-)

Next change your geek tool command from File to Shell and make it simply:

cat /var/tmp/top.log

Note that there is nothing which prevents cat from getting a completely empty file if it reads at the wrong time, but that doesn't seem to happy too often for me.

j.

[ Reply to This | # ]