forked from openinterpreter/01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_utils.py
More file actions
28 lines (25 loc) · 1.16 KB
/
Copy pathprocess_utils.py
File metadata and controls
28 lines (25 loc) · 1.16 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
import os
import psutil
import signal
def kill_process_tree():
pid = os.getpid() # Get the current process ID
try:
# Send SIGTERM to the entire process group to ensure all processes are targeted
os.killpg(os.getpgid(pid), signal.SIGKILL)
parent = psutil.Process(pid)
children = parent.children(recursive=True)
for child in children:
print(f"Forcefully terminating child PID {child.pid}")
child.kill() # Forcefully kill the child process immediately
gone, still_alive = psutil.wait_procs(children, timeout=3)
if still_alive:
for child in still_alive:
print(f"Child PID {child.pid} still alive, attempting another kill")
child.kill()
print(f"Forcefully terminating parent PID {pid}")
parent.kill() # Forcefully kill the parent process immediately
parent.wait(3) # Wait for the parent process to terminate
except psutil.NoSuchProcess:
print(f"Process {pid} does not exist or is already terminated")
except psutil.AccessDenied:
print(f"Permission denied to terminate some processes")