-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkill_evaluate_processes.py
More file actions
53 lines (45 loc) · 1.75 KB
/
kill_evaluate_processes.py
File metadata and controls
53 lines (45 loc) · 1.75 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
import subprocess
import os
import time
def kill_processes(process_name):
try:
# Get all processes
cmd = "ps -ef"
output = subprocess.check_output(cmd, shell=True).decode().split('\n')
pids_to_kill = []
for line in output:
if process_name in line and 'grep' not in line:
parts = line.split()
if len(parts) > 1:
pid = parts[1]
pids_to_kill.append(pid)
if not pids_to_kill:
print(f"No processes found for '{process_name}'.")
return
print(f"Found processes to kill: {pids_to_kill}")
for pid in pids_to_kill:
try:
os.kill(int(pid), 9) # SIGKILL
print(f"Killed process {pid}")
except OSError as e:
print(f"Error killing process {pid}: {e}")
# Verify if processes are killed
time.sleep(1) # Give some time for processes to terminate
remaining_processes = []
cmd = "ps -ef"
output = subprocess.check_output(cmd, shell=True).decode().split('\n')
for line in output:
if process_name in line and 'grep' not in line:
remaining_processes.append(line)
if remaining_processes:
print(f"Some processes for '{process_name}' are still running:")
for line in remaining_processes:
print(line)
else:
print(f"All processes for '{process_name}' have been killed.")
except subprocess.CalledProcessError as e:
print(f"Error running ps command: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
kill_processes("evaluate.py")