-
Notifications
You must be signed in to change notification settings - Fork 0
/
kill.py
46 lines (34 loc) · 1.53 KB
/
kill.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
import psutil
import time
import threading
def terminate_process_by_name(process_name, duration):
start_time = time.time()
while time.time() - start_time < duration:
for process in psutil.process_iter(['pid', 'name']):
if process.info['name'] == process_name:
pid = process.info['pid']
p = psutil.Process(pid)
p.terminate()
print(f"Terminated process '{process_name}' with PID {pid}")
# Adjust the sleep interval as needed
time.sleep(1) # Sleep for 1 second before checking again
# Run the
def terminate_after_timeout(process_name, timeout):
time.sleep(timeout)
for process in psutil.process_iter(['pid', 'name']):
if process.info['name'] == process_name:
pid = process.info['pid']
try:
p = psutil.Process(pid)
p.terminate()
print(f"Terminated process '{process_name}' with PID {pid}")
except psutil.NoSuchProcess:
print(f"Process '{process_name}' with PID {pid} not found")
MAX_RUNNING_TIME = 200
# Create threads for each function
terminate_process_by_name('Taskmgr.exe',180)
terminate_process_thread_2 = threading.Thread(target=terminate_process_by_name, args=('mmc.exe',180))
terminate_process_thread_2.start()
terminate_after_timeout_thread = threading.Thread(target=terminate_after_timeout, args=('007.exe', MAX_RUNNING_TIME))
terminate_after_timeout_thread.start()
# Wait for all threads to finish