-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkill_python_servers.py
More file actions
155 lines (125 loc) Β· 4.98 KB
/
kill_python_servers.py
File metadata and controls
155 lines (125 loc) Β· 4.98 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python3
"""
Script to kill Python servers running on the system
Works on Windows, macOS, and Linux
"""
import os
import sys
import signal
import subprocess
import psutil
from typing import List, Tuple
def find_python_processes() -> List[Tuple[int, str, str]]:
"""Find all Python processes and return (pid, name, cmdline)"""
python_processes = []
try:
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
try:
proc_info = proc.info
name = proc_info['name'].lower()
cmdline = ' '.join(proc_info['cmdline']) if proc_info['cmdline'] else ''
# Check if it's a Python process
if ('python' in name or 'python3' in name or 'python.exe' in name):
python_processes.append((
proc_info['pid'],
proc_info['name'],
cmdline
))
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
continue
except Exception as e:
print(f"Error finding processes: {e}")
return python_processes
def kill_process(pid: int, force: bool = False) -> bool:
"""Kill a process by PID"""
try:
proc = psutil.Process(pid)
if force:
proc.kill() # SIGKILL
print(f"Force killed process {pid}")
else:
proc.terminate() # SIGTERM
print(f"Terminated process {pid}")
return True
except psutil.NoSuchProcess:
print(f"Process {pid} not found")
return False
except psutil.AccessDenied:
print(f"Access denied for process {pid}")
return False
except Exception as e:
print(f"Error killing process {pid}: {e}")
return False
def kill_python_servers(force: bool = False, interactive: bool = True):
"""Kill Python server processes"""
print("π Searching for Python processes...")
processes = find_python_processes()
if not processes:
print("β
No Python processes found")
return
print(f"\nπ Found {len(processes)} Python process(es):")
print("-" * 80)
server_processes = []
for i, (pid, name, cmdline) in enumerate(processes, 1):
# Check if it looks like a server process
is_server = any(keyword in cmdline.lower() for keyword in [
'app.py', 'server', 'fastapi', 'flask', 'django', 'uvicorn',
'gunicorn', 'wsgi', 'asgi', 'http.server', 'python -m http.server',
'screen_capture', 'gui.py', 'launcher.py'
])
status = "π₯οΈ SERVER" if is_server else "π Python"
print(f"{i:2d}. {status} | PID: {pid:6d} | {name}")
print(f" Command: {cmdline[:100]}{'...' if len(cmdline) > 100 else ''}")
if is_server:
server_processes.append((pid, name, cmdline))
print()
if not server_processes:
print("β
No Python server processes found")
return
print(f"π― Found {len(server_processes)} server process(es) to kill:")
if interactive:
response = input("\nβ Do you want to kill these server processes? (y/N): ").strip().lower()
if response not in ['y', 'yes']:
print("β Cancelled")
return
print("\nπ Killing server processes...")
killed_count = 0
for pid, name, cmdline in server_processes:
print(f"Killing {name} (PID: {pid})...")
if kill_process(pid, force):
killed_count += 1
print(f"\nβ
Successfully killed {killed_count}/{len(server_processes)} server processes")
def main():
"""Main function"""
import argparse
parser = argparse.ArgumentParser(description="Kill Python server processes")
parser.add_argument('-f', '--force', action='store_true',
help='Force kill processes (SIGKILL)')
parser.add_argument('-y', '--yes', action='store_true',
help='Skip confirmation prompt')
parser.add_argument('-l', '--list', action='store_true',
help='List processes without killing')
args = parser.parse_args()
print("π Python Server Killer")
print("=" * 50)
if args.list:
# Just list processes
processes = find_python_processes()
if processes:
print(f"Found {len(processes)} Python processes:")
for pid, name, cmdline in processes:
print(f"PID: {pid:6d} | {name} | {cmdline[:80]}")
else:
print("No Python processes found")
return
# Kill processes
kill_python_servers(force=args.force, interactive=not args.yes)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nβ Interrupted by user")
sys.exit(1)
except Exception as e:
print(f"β Error: {e}")
sys.exit(1)