Skip to content

Instantly share code, notes, and snippets.

@pwwang
Last active November 4, 2023 01:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwwang/2faa1799958b0337949a94bc777802e1 to your computer and use it in GitHub Desktop.
Save pwwang/2faa1799958b0337949a94bc777802e1 to your computer and use it in GitHub Desktop.
qstat with full job name
#!/usr/bin/env python
from subprocess import check_output
from re import search
cmd = ['qstat', '-xml']
# get the xml output
output = check_output (cmd)
keys = [] # the feature names
vals = [] # the jobs including all features
job = [] # the features
haskey = 0 # whether I have the extra 'state' key
lens = [] # str length of different field
states = {} # counting jobs with different state
for line in output.split("\n"):
# The start line of the job
m1 = search (r'<job_list state="(.+)"', line)
# The feature line
m2 = search (r'<(.+?)>(.*?)</.+>', line)
if m1:
# Get the state
state = m1.group(1)
job = [state]
# Count
if state not in states:
states[state] = 0
states[state] += 1
# Add the job
vals.append(job)
# If 'state' key is not added
if haskey == 0:
keys = ['state']
haskey = 1
# If it's already added
elif haskey == 1:
haskey = 2
elif m2:
# The feature name and value
title = m2.group(1)
value = m2.group(2)
# Make sure 'state' is the last feature
col = 0 if len (job) == 1 else -1
# job is a reference, referred to the job in vals
job.insert (col, value)
if haskey == 1:
# Make sure 'state' is the last key
col = 0 if len (keys) == 1 else -1
keys.insert (col, title)
# calculate the length
for i, key in enumerate(keys):
klen = len (key)
vlen = max (map(lambda x: len(x[i]), vals))
lens.append (max(klen, vlen))
print " ".join ([x.upper().ljust(lens[i]) for i,x in enumerate(keys)])
print "+".join (['-'*(lens[i]+1) for i,_ in enumerate(keys)])
for val in vals:
print " ".join ([x.ljust(lens[i]) for i,x in enumerate(val)])
print "\nTotal jobs: %s%s" % (len(vals), "".join([", " + k + ": " + str(v) for k,v in states.items()]))
@pwwang
Copy link
Author

pwwang commented Jul 6, 2017

How the output looks like:

JB_JOB_NUMBER  JAT_PRIO  JB_NAME                           JB_OWNER  STATE  JAT_START_TIME       QUEUE_NAME               SLOTS  STATE  
--------------+---------+---------------------------------+---------+------+--------------------+------------------------+------+--------
6802833        0.50000   pTransformBam.notag.2NmxRNIA.15   user      r      2017-07-05T14:43:11  1-day@dnode37.xxxx.edu   1      running
6802907        0.50000   pTransformBam.notag.2NmxRNIA.111  user      r      2017-07-05T14:43:12  1-day@dnode82.xxxx.edu   1      running
6802910        0.50000   pTransformBam.notag.2NmxRNIA.101  user      r      2017-07-05T14:43:12  1-day@dnode132.xxxx.edu  1      running
6807364        0.50000   pTransformBam.notag.2NmxRNIA.408  user      r      2017-07-05T16:23:11  1-day@dnode204.xxxx.edu  1      running

Total jobs: 400, running: 200, pending: 200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment