This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqdel_all.py
executable file
·62 lines (51 loc) · 1.65 KB
/
qdel_all.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
#!/usr/bin/python
'''
Script uses the std output of the qstat command to delete either, all pbs jobs,
only queued pbs jobs or only running pbs jobs.
'''
import sys
import subprocess as sp
def query_yesno(question):
valid_yes = ['yes','y']
valid_no = ['no','n']
while True:
sys.stdout.write(question + ' [yes/no]: ') #no newline
answer = raw_input().lower()
if (answer in valid_yes) or (answer in valid_no):
return (answer in valid_yes)
print('Please enter \'yes\' or \'no\'.')
# Delete all, queued or running?
jtypes = {'all':'all','a':'all','queued':'queued','q':'queued','running':'running','r':'running'}
jtype = 'all' # default
if len(sys.argv)==2:
jtype = jtypes[sys.argv[1].lower()]
elif len(sys.argv)>2:
print('Usage: qdel_all [JOBTYPE=all]')
print(' where JOBTYPE is either \'all\' (a),\'queued\' (q) or \'running\' (r)')
jtypestr = ''
if jtype=='queued':
jtypestr = 'queued '
elif jtype=='running':
jtypestr = 'running '
# Safety check before continuing
q_string = 'This will delete all '+jtypestr+'PBS jobs. Do you wish to continue?'
if not query_yesno(q_string):
exit(0)
# Get info on PBS jobs
qstat = sp.Popen(['qstat','-a'],stdout=sp.PIPE)
# Clear header and info lines
for line in qstat.stdout:
if line[0]!='-':
del line
else:
del line
break
# Get job id
for line in qstat.stdout:
data = line.split()
jobid = data[0]
jobstate = data[9]
if jtype=='all' or (jtype=='queued' and jobstate=='Q') or (jtype=='running' and jobstate=='R'):
sp.call(['qdel',data[0]])
# Print final message
print('Deleted all '+jtypestr+'PBS jobs.')