Skip to content

Instantly share code, notes, and snippets.

@mttmantovani
Last active January 21, 2021 16:33
Show Gist options
  • Save mttmantovani/b925d3980204710bccf432453a873ca0 to your computer and use it in GitHub Desktop.
Save mttmantovani/b925d3980204710bccf432453a873ca0 to your computer and use it in GitHub Desktop.
Remotely check of SGE cluster jobs through SSH using paramiko
#!/usr/bin/env python3
import paramiko
import os
import warnings
warnings.filterwarnings('ignore')
keyfile = os.path.expanduser('~')+'/.ssh/id_rsa.pub'
cred = {"username": "mattia",
"hostname": "scc.uni-konstanz.de",
"key_filename": keyfile,
}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(**cred)
stdin, stdout, stderr = ssh.exec_command("qstat -s sr")
jobs = [j.strip().split()[0] for j in stdout.readlines()[3:-3]]
for job in jobs:
stdin, stdout, stderr = ssh.exec_command(f"qstat -j {job} | egrep 'cwd|job_name'")
lines = stdout.readlines()
cwd = lines[0].strip().split()[1]
name = lines[1].strip().split()[1]
stdin, stdout, stderr = ssh.exec_command(f"tail {cwd}/{name}.o{job}")
out = stdout.readlines()
print(f"OUT LOG for job {job}:")
print("=====================")
print(*out)
stdin, stdout, stderr = ssh.exec_command(f"tail -n 5 {cwd}/{name}.e{job}")
err = stdout.readlines()
print(f"ERR LOG for job {job}:")
print("=====================")
print(*err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment