@@ -50,6 +50,7 @@ def find_ancestor(pid=None, username=None):
50
50
for parent in reversed (parents ):
51
51
if parent .username () == username :
52
52
return parent
53
+ return process
53
54
54
55
55
56
def get_cmdline (process ):
@@ -128,21 +129,6 @@ def process_status(process, metrics):
128
129
return ',' .join (status )
129
130
130
131
131
- def get_filename_suffix (file_id ):
132
- filename = ''
133
- if 'PBS_JOBID' in os .environ :
134
- filename += f'_{ os .environ ["PBS_JOBID" ]} '
135
- if 'PBS_ARRAYID' in os .environ :
136
- filename += f'_{ os .environ ["PBS_ARRAYJOBID" ]} '
137
- if file_id :
138
- filename += f'_{ file_id } '
139
- return f'{ filename } .txt'
140
-
141
-
142
- def get_filename (file_id ):
143
- return platform .node () + get_filename_suffix (file_id )
144
-
145
-
146
132
def main ():
147
133
arg_parser = ArgumentParser (description = 'monitor processes' )
148
134
arg_parser .add_argument ('--pid' , type = int , help = 'parent process ID ot monitor' )
@@ -152,33 +138,32 @@ def main():
152
138
arg_parser .add_argument ('--affinity' , action = 'store_true' ,
153
139
help = 'monitor process affinity' )
154
140
arg_parser .add_argument ('--files' , action = 'store_true' , help = 'monitor poen files' )
155
- output_group = arg_parser .add_mutually_exclusive_group ()
156
- output_group .add_argument ('--output-file' , help = 'name of file to store informatoin' )
157
- output_group .add_argument ('--stdout' , action = 'store_true' ,
158
- help = 'output to standard output' )
159
- output_group .add_argument ('--file_id' , help = 'identification string for output filename' )
141
+ arg_parser .add_argument ('--ancestor' , action = 'store_true' ,
142
+ help = 'search for ancestor owned by use and report on all its decendants' )
143
+ arg_parser .add_argument ('--output-file' , help = 'name of file to store informatoin' )
160
144
options = arg_parser .parse_args ()
161
- ancestor = find_ancestor (options .pid , options .user )
145
+ if options .ancestor :
146
+ process = find_ancestor (options .pid , options .user )
147
+ else :
148
+ process = psutil .Process (options .pid )
162
149
inactive = []
163
150
if not options .affinity :
164
151
inactive .append ('affinity' )
165
152
if not options .files :
166
153
inactive .append ('read_files' )
167
154
inactive .append ('write_files' )
168
155
metrics = define_actions (inactive )
169
- if options .stdout :
170
- file = sys .stdout
171
- elif options .output_file :
156
+ if options .output_file :
172
157
file = open (options .output_file , 'w' )
173
158
else :
174
- file = open ( get_filename ( options . file_id ), 'w' )
159
+ file = sys . stdout
175
160
try :
176
161
with file :
177
162
print (status_header (metrics ), file = file )
178
163
while True :
179
- process_info = [process_status (ancestor , metrics )]
180
- for process in ancestor .children (recursive = True ):
181
- process_info .append (process_status (process , metrics ))
164
+ process_info = [process_status (process , metrics )]
165
+ for child_process in process .children (recursive = True ):
166
+ process_info .append (process_status (child_process , metrics ))
182
167
print ('\n ' .join (process_info ), file = file )
183
168
time .sleep (options .delta )
184
169
except KeyboardInterrupt :
0 commit comments