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 08:10:47PM
Here's a small optimzation with a different invocation of top. No need to limit top's output to 45 lines, -F to less its CPU load, and different sorting. Also don't need the timeout in the select():
#!/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 -s3 -S -F -ocpu -Otime")
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], [], [])
	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



[ Reply to This | # ]