Skip to content

Instantly share code, notes, and snippets.

@victorhcm
Last active January 22, 2022 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorhcm/72a81782bf2e89c5100955643f977a5a to your computer and use it in GitHub Desktop.
Save victorhcm/72a81782bf2e89c5100955643f977a5a to your computer and use it in GitHub Desktop.
A slurm sbatch wrapper that automatically opens the output file
#!/bin/python3
import os
import sys
import subprocess
import re
def run():
arguments = sys.argv[1:]
process = subprocess.run(['sbatch', *arguments],
stdout=subprocess.PIPE,
universal_newlines=True)
jobinfo = process.stdout.strip()
jobid = jobinfo.split()[-1]
print(jobinfo)
process = subprocess.run(['scontrol', 'show', 'job', jobid],
stdout=subprocess.PIPE,
universal_newlines=True)
# splits consecutive spaces, spaces, =, \n
scontrol = re.split(' +| |=|\n', process.stdout.strip())
index = scontrol.index("StdOut")
fileout = scontrol[index + 1].strip()
print(f'Waiting for {fileout}... ', end='')
while not os.path.exists(fileout):
pass
print('Done!\n'
'Output:\n')
os.system(f'tail -f {fileout}')
def usage():
print('sbatch wrapper that automatically opens the output file\n\n'
'Usage:\n'
'$ sbatlog <script.sh> <--any-additional-slurm-commands>')
if __name__ == '__main__':
if len(sys.argv) > 1:
run()
else:
usage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment