How can I get qstat
to give me full job names?
I know qstat -r
gives detailed information about the task, but it\'s too much and the resource r
A poor KISS solution :
qstat -xml -f -u \* | fgrep JB_name | wc -l
This on is a bit messy, but it works as a simple solution to have in the command history. All standard tools. Output is pretty much the same as what you get from a normal qstat call, but you won't get the headers:
One-liner:
qstat -xml | tr '\n' ' ' | sed 's#<job_list[^>]*>#\n#g' \
| sed 's#<[^>]*>##g' | grep " " | column -t
Description of commands:
List jobs as XML:
qstat -xml
Remove all newlines:
tr '\n' ' '
Add newline before each job entry in the list:
sed 's#<job_list[^>]*>#\n#g'
Remove all XML stuff:
sed 's#<[^>]*>##g'
Hack to add newline at the end:
grep " "
Columnize:
column -t
Example output
351996 0.50502 ProjectA_XXXXXXXXX_XXXX_XXXXXX user123 r 2015-06-25T15:38:41 xxxxx-sim01@xxxxxx02.xxxxx.xxx 1
351997 0.50502 ProjectA_XXX_XXXX_XXX user123 r 2015-06-25T15:39:26 xxxxx-sim01@xxxxxx23.xxxxx.xxx 1
351998 0.50502 ProjectA_XXXXXXXXXXXXX_XXXX_XXXX user123 r 2015-06-25T15:40:26 xxxxx-sim01@xxxxxx14.xxxxx.xxx 1
351999 0.50502 ProjectA_XXXXXXXXXXXXXXXXX_XXXX_XXXX user123 r 2015-06-25T15:42:11 xxxxx-sim01@xxxxxx19.xxxxx.xxx 1
352001 0.50502 ProjectA_XXXXXXXXXXXXXXXXXXXXXXX_XXXX_XXXX user123 r 2015-06-25T15:42:11 xxxxx-sim01@xxxxxx11.xxxxx.xxx 1
352008 0.50501 runXXXX69 usr1 r 2015-06-25T15:49:04 xxxxx-sim01@xxxxxx17.xxxxx.xxx 1
352009 0.50501 runXXXX70 usr1 r 2015-06-25T15:49:04 xxxxx-sim01@xxxxxx01.xxxxx.xxx 1
352010 0.50501 runXXXX71 usr1 r 2015-06-25T15:49:04 xxxxx-sim01@xxxxxx06.xxxxx.xxx 1
352011 0.50501 runXXXX72 usr1 r 2015-06-25T15:49:04 xxxxx-sim01@xxxxxx21.xxxxx.xxx 1
352012 0.50501 runXXXX73 usr1 r 2015-06-25T15:49:04 xxxxx-sim01@xxxxxx13.xxxxx.xxx 1
352013 0.50501 runXXXX74 usr1 r 2015-06-25T15:49:04 xxxxx-sim01@xxxxxx11.xxxxx.xxx 1
For me the script of Physical Chemist didn't work so I wrote a very simple script using the xml.tree.ElementTree module which i regard as somewhat easier than xml.dom.minidom
import os
import xml.etree.ElementTree as ET
f = os.popen('qstat -x')
tree = ET.parse(f)
root = tree.getroot()
print "Job_Id walltime state nodes Job_Name"
print "------ -------- ----- --------------- --------------------------"
for job in root:
print job.find('Job_Id').text, " ",
print job.find('resources_used').find('walltime').text, " ",
print job.find('job_state').text, " ",
print job.find('Resource_List').find('nodes').text, " ",
print job.find('Job_Name').text
python code
import xmltodict
import subprocess as sp
import pandas as pd
qstat_xml = sp.check_output(['qstat','--xml'], stderr=sp.STDOUT) # read xml
stat_dict = xmltodict.parse(qstat_xml) # convert to dict
job_list = stat_dict['Data']['Job'] # select job_list
job_df = pd.DataFrame(job_list) # convert to dataframe
print('columns', job_df.columns) # print available columns
column_list = ['Job_Id', 'Job_Name']
selection_df = job_df[column_list] # select columns
print(selection_df)
Thanks to JLT for nice simple code. I've expanded it a bit to fit my needs and make it look nice.
Sample Output:
Job ID Job Name Owner Status
------ ------------------------------------ ------ ------
201716 AtacSilN100400K mtsige R
201771 IsoOnGrap400K mtsige R
202067 AtacOnSilica400K mtsige R
202100 AtacGrapN100400K mtsige R
202135 AtacOnSilc400K mtsige R
202145 AtacOnGrap400K mtsige R
202152 AtacOnGraphN3360K mtsige R
202161 AtacticSilicaN10 mtsige R
202163 AtacGrapN10 mtsige R
202169 AtacSilcN10 mtsige R
202192 wallpmma07 am110 R
202193 wallpmma03 am110 R
202194 att03wpm_95solps am110 R
202202 AtacticSilicaN3 mtsige R
203260 8test18_trop_2p ico R
203359 parseAll_Bob/Sub951By50/Cyl20A_atom1 oge1 R
203360 parseAll_Bob/Sub951By50/Cyl30A_atom1 oge1 R
203361 parseAll_Bob/Sub951By50/Cyl30A_atom2 oge1 R
Code:
#!/opt/bin/python3
import os
import xml.etree.ElementTree as ET
#Fields
fields=['Job_Id','Job_Name','Job_Owner','job_state']
names=['Job ID','Job Name','Owner','Status']
#Get job info
f = os.popen('qstat -x')
tree = ET.parse(f)
root = tree.getroot()
n_fields=len(fields)
jobs=[[job.find(field).text for field in fields] for job in root]
max_lengths=[len(name) for name in names]
sep=' '
#Identify max characer length per field
for j in jobs:
for i in range(n_fields):
#Chop off anything after and including '@' or '.' from all fields
if j[i].find('@')>0:
j[i]=j[i][:j[i].find('@')]
if j[i].find('.')>0:
j[i]=j[i][:j[i].find('.')]
if(len(j[i])>max_lengths[i]):
max_lengths[i]=len(j[i])
#Field names
for i in range(n_fields):
print('{s:^{length}}'.format(s=names[i],length=max_lengths[i]),end=sep)
print()
#Dashes
for i in range(n_fields):
print('-'*max_lengths[i],end=sep)
print()
#Jobs
for j in jobs:
for i in range(n_fields):
if j[i].find('@')>0:
j[i]=j[i][:j[i].find('@')]
print('{s:<{length}}'.format(s=j[i],length=max_lengths[i]),end=sep)
print()
This script works pretty well. It looks like it is from cambridge. http://www.hep.ph.ic.ac.uk/~dbauer/grid/myqstat.py
For Python 3:
#!/usr/bin/python
import xml.dom.minidom
import os
import sys
import string
f=os.popen('qstat -u \* -xml -r')
dom=xml.dom.minidom.parse(f)
jobs=dom.getElementsByTagName('job_info')
run=jobs[0]
runjobs=run.getElementsByTagName('job_list')
def fakeqstat(joblist):
for r in joblist:
try:
jobname=r.getElementsByTagName('JB_name')[0].childNodes[0].data
jobown=r.getElementsByTagName('JB_owner')[0].childNodes[0].data
jobstate=r.getElementsByTagName('state')[0].childNodes[0].data
jobnum=r.getElementsByTagName('JB_job_number')[0].childNodes[0].data
jobtime='not set'
if(jobstate=='r'):
jobtime=r.getElementsByTagName('JAT_start_time')[0].childNodes[0].data
elif(jobstate=='dt'):
jobtime=r.getElementsByTagName('JAT_start_time')[0].childNodes[0].data
else:
jobtime=r.getElementsByTagName('JB_submission_time')[0].childNodes[0].data
print(jobnum, '\t', jobown.ljust(16), '\t', jobname.ljust(16),'\t', jobstate,'\t',jobtime)
except Exception as e:
print(e)
fakeqstat(runjobs)
For Python 2:
#!/usr/bin/python
import xml.dom.minidom
import os
import sys
import string
#import re
f=os.popen('qstat -u \* -xml -r')
dom=xml.dom.minidom.parse(f)
jobs=dom.getElementsByTagName('job_info')
run=jobs[0]
runjobs=run.getElementsByTagName('job_list')
def fakeqstat(joblist):
for r in joblist:
jobname=r.getElementsByTagName('JB_name')[0].childNodes[0].data
jobown=r.getElementsByTagName('JB_owner')[0].childNodes[0].data
jobstate=r.getElementsByTagName('state')[0].childNodes[0].data
jobnum=r.getElementsByTagName('JB_job_number')[0].childNodes[0].data
jobtime='not set'
if(jobstate=='r'):
jobtime=r.getElementsByTagName('JAT_start_time')[0].childNodes[0].data
elif(jobstate=='dt'):
jobtime=r.getElementsByTagName('JAT_start_time')[0].childNodes[0].data
else:
jobtime=r.getElementsByTagName('JB_submission_time')[0].childNodes[0].data
print jobnum, '\t', jobown.ljust(16), '\t', jobname.ljust(16),'\t', jobstate,'\t',jobtime
fakeqstat(runjobs)