-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyqdel.py
89 lines (72 loc) · 2.92 KB
/
pyqdel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
import sys
import subprocess
import time
import xml.etree.ElementTree as ET
import paramiko
def pyqdel(jobid: int):
result = subprocess.Popen(["qstat", "-x", str(jobid)],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
xml = result.communicate()[0].decode("utf-8")
# print(xml)
if xml == "":
raise Exception(f"not found jobid : {jobid}")
# parse check
root = ET.fromstring(xml)
# print_element(root)
try:
print("Job/Resource_List/exec_host : ",
root.find("Job").find("exec_host").text)
print("Job/euser : ", root.find("Job").find("euser").text)
print("Job/Job_Name : ", root.find("Job").find("Job_Name").text)
except:
result = subprocess.Popen(["qdel", str(jobid)],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return
# user check
result = subprocess.Popen(["whoami"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
myname = result.communicate()[0].decode("utf-8").strip()
if myname != root.find("Job").find("euser").text:
print(myname)
raise Exception(f"the owner of job {jobid} is not me!")
# qdel
result = subprocess.Popen(["qdel", str(jobid)],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(2)
nodes = root.find("Job").find("exec_host").text.split("+")
job_name = root.find("Job").find("Job_Name").text
for node in nodes:
node_info = node.split("/")
print(node_info)
with paramiko.SSHClient() as ssh:
# 初回ログイン時に「Are you sure you want to continue connecting (yes/no)?」と
# きかれても問題なく接続できるように。
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# ssh接続
ssh.connect(node_info[0])
stdin, stdout, stderr = ssh.exec_command("ps xg | grep qdeal")
for o in stdout:
print('[std]', o, end='')
cmd = "ps aux | grep {} | grep -v grep | awk '{{ print \"kill -9\", $2 }}' | sh".format(
job_name)
# コマンド実行
stdin, stdout, stderr = ssh.exec_command(cmd)
# コマンド実行後に標準入力が必要な場合
# stdin.write('password\n')
# stdin.flush()
# 実行結果を表示
for o in stdout:
print('stdout : ', o, end='')
for e in stderr:
print('stderr : ', e, end='')
return
def print_element(element, path="/"):
print(path + element.tag, ":", element.text)
for sub in element:
print_element(sub, path + element.tag + "/")
if __name__ == "__main__":
if len(sys.argv) < 2:
raise Exception("Need to input Job ID")
for jobid in sys.argv[1:]:
pyqdel(int(jobid))