Skip to content

Instantly share code, notes, and snippets.

@JonnoFTW
Created April 5, 2018 03:07
Show Gist options
  • Save JonnoFTW/11389ecfec0ceda67a554f7650cf9cef to your computer and use it in GitHub Desktop.
Save JonnoFTW/11389ecfec0ceda67a554f7650cf9cef to your computer and use it in GitHub Desktop.
A python curses script to interactively view SGE qstat output
#!/usr/bin/env python
import curses
import subprocess
import sys
from datetime import datetime
AUTHOR = "Jonathan Mackenzie"
NAME = "pqstat"
def get_qstat(args):
return subprocess.Popen(['qstat']+args,stdout=subprocess.PIPE).communicate()[0]
def main(scr):
scr.nodelay(1)
scr.clear()
curses.curs_set(0)
try:
while True:
txt = get_qstat(sys.argv[1:])
now = datetime.now()
scr.erase()
height, width = scr.getmaxyx()
tstr = now.strftime("%A %d %B %H:%M:%S")
scr.addstr(0, 0, tstr, curses.A_BOLD)
scr.addstr(0, len(tstr)+2, "q to exit")
msg = NAME + " by " + AUTHOR
scr.addstr(0, width-len(msg), msg, curses.A_BOLD)
yidx = 1
for line in txt.splitlines():
if yidx >= height:
continue
line = line.strip()[:width]
if all(map(lambda x:x=='-',line)):
continue
if yidx == 1:
scr.addstr(yidx,0,line, curses.A_BOLD)
else:
scr.addstr(yidx,0,line)
yidx += 1
scr.refresh()
curses.delay_output(1000)
if scr.getch() == ord('q'):
return None
except KeyboardInterrupt:
return None
except Exception as e:
return e
if __name__ == "__main__":
out = curses.wrapper(main)
if out is not None:
print("An error occurred")
print(out.message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment