-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher.py
More file actions
26 lines (22 loc) · 790 Bytes
/
watcher.py
File metadata and controls
26 lines (22 loc) · 790 Bytes
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
import psutil
import time
import os
TIMEOUT = 600
def killer():
instances = []
now = time.time()
for p in psutil.process_iter(attrs=['pid', 'name', 'cmdline', 'create_time']):
if p.info['cmdline'] and ('--result_file' in p.info['cmdline'] or '--results_file' in p.info['cmdline']) and p.info['name'] == 'python3':
instances.append((p.info['pid'], now - p.info['create_time']))
print(f'[!] {TIMEOUT=} {len(instances)=} running {instances=}')
for (pid, runtime) in instances:
if runtime > TIMEOUT:
print('Kill instance:', pid)
os.system(f'kill -9 {pid}')
if __name__ == "__main__":
while True:
try:
killer()
except Exception as e:
print(f'[!] {e=}')
time.sleep(10)