-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_killer.py
More file actions
53 lines (39 loc) · 1.45 KB
/
Copy pathprocess_killer.py
File metadata and controls
53 lines (39 loc) · 1.45 KB
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
"""
process_killer.py
Kill sleeping processes.
"""
import argparse
import os
import sys
import psutil
def write_all_processes():
temp = sys.stdout # store original stdout object for later
sys.stdout = open('out.txt', 'w') # redirect all prints to this log file
psutil.test()
sys.stdout.close()
sys.stdout = temp # restore print commands to interactive prompt
def parse_process_file(file_name):
with open('out.txt', 'r') as f:
lines = f.read().split('\n')
# Fetch all the sleeping processes that contain the file-name
sleeping_processes = []
for line in lines:
if (' sleep ' in line) and (file_name in line): sleeping_processes.append(line)
# Parse out the PIDs and terminate the processes
for sp in sleeping_processes:
sp = [s for s in sp.split(' ') if len(s) > 0]
pid = int(sp[1]) # Second item contains the PID
p = psutil.Process(pid) # Get control of the sleeping process
p.terminate() # Terminate the process
# Close the process-file
os.remove('out.txt')
def main(file_name):
"""Run the process-killer."""
write_all_processes()
parse_process_file(file_name)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='')
parser.add_argument('--file_name', type=str, default='main.py')
args = parser.parse_args()
# Run the process
main(args.file_name)