Last active
January 22, 2022 19:09
-
-
Save victorhcm/72a81782bf2e89c5100955643f977a5a to your computer and use it in GitHub Desktop.
A slurm sbatch wrapper that automatically opens the output file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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