Skip to content

Instantly share code, notes, and snippets.

@stevekm
Created December 5, 2018 22:59
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 stevekm/7831fac98473ea17d781330baa0dd7aa to your computer and use it in GitHub Desktop.
Save stevekm/7831fac98473ea17d781330baa0dd7aa to your computer and use it in GitHub Desktop.
Convert SLURM squeue output to JSON format
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Convert SLURM squeue output to JSON format
"""
import subprocess as sp
import json
process = sp.Popen(['squeue', '-o', '%all'], stdout = sp.PIPE, stderr = sp.PIPE, shell = False, universal_newlines = True)
proc_stdout, proc_stderr = process.communicate()
lines = proc_stdout.split('\n')
header_line = lines.pop(0)
header_cols = header_line.split('|')
entries = []
error_lines = [] # do something with this later
for line in lines:
parts = line.split('|')
d = {}
if len(parts) != len(header_cols):
error_lines.append((len(parts), line, parts))
else:
for i, key in enumerate(header_cols):
d[key] = parts[i]
entries.append(d)
print(json.dumps(entries, indent = 2))
@williampiat3
Copy link

I used this piece of code however I noticed that entries were duplicated
line 24 there is an indent that shouldn't be there if you only want one copy of the job information (the append should be outside the loop)
Cheers!

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