This repository was archived by the owner on Mar 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkill_orphaned_aggressive.py
More file actions
129 lines (105 loc) · 3.99 KB
/
kill_orphaned_aggressive.py
File metadata and controls
129 lines (105 loc) · 3.99 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
"""Aggressive orphaned process killer - removes idle console and Node processes"""
import psutil
import time
from datetime import datetime
from pathlib import Path
class AggressiveOrphanKiller:
"""Aggressively kills idle console and Node processes"""
# These are the targets we WANT to kill if idle
KILL_TARGETS = {
'node.exe', 'conhost.exe', 'cmd.exe', 'powershell.exe',
'node', 'npm', 'npx', 'npm.cmd', 'conhost'
}
# Only protect these critical system processes
PROTECTED = {
'svchost.exe', 'csrss.exe', 'lsass.exe', 'services.exe',
'smss.exe', 'wininit.exe', 'winlogon.exe', 'explorer.exe',
'dwm.exe', 'taskhostw.exe', 'python.exe', 'vscode.exe',
'cursor.exe', 'kiro.exe'
}
def __init__(self):
self.killed = []
self.protected = []
self.log_file = Path("data/aggressive_kill_log.txt")
def is_idle(self, proc):
"""Check if process is idle"""
try:
# Get CPU times
cpu_times = proc.cpu_times()
# If zero CPU usage, it's idle
if cpu_times.user == 0 and cpu_times.system == 0:
return True
# Check I/O
try:
io = proc.io_counters()
if io.read_bytes == 0 and io.write_bytes == 0:
return True
except:
pass
return False
except:
return False
def kill_process(self, proc):
"""Kill a process"""
try:
pid = proc.pid
name = proc.name()
proc.terminate()
time.sleep(0.1)
# If still alive, force kill
if proc.is_running():
proc.kill()
self.killed.append((pid, name))
self._log(pid, name, "KILLED")
return True
except Exception as e:
self._log(proc.pid, proc.name(), f"FAILED: {e}")
return False
def _log(self, pid, name, status):
"""Log action"""
try:
self.log_file.parent.mkdir(parents=True, exist_ok=True)
with open(self.log_file, 'a') as f:
f.write(f"{datetime.now().isoformat()} | {status} | PID {pid} | {name}\n")
except:
pass
def run(self):
"""Run the killer"""
print("=" * 80)
print("AGGRESSIVE ORPHANED PROCESS KILLER")
print("=" * 80)
print("\n[SCANNING] Looking for idle processes...")
candidates = []
for proc in psutil.process_iter(['pid', 'name']):
try:
name = proc.info['name'].lower()
# Check if it's a target
if not any(target in name for target in self.KILL_TARGETS):
continue
# Check if it's protected
if any(protected in name for protected in self.PROTECTED):
self.protected.append((proc.info['pid'], proc.info['name']))
continue
# Check if idle
if self.is_idle(proc):
candidates.append(proc)
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
print(f"Found {len(candidates)} idle processes to kill")
print(f"Protected {len(self.protected)} critical processes\n")
if candidates:
print("[KILLING] Terminating idle processes...")
for proc in candidates:
try:
self.kill_process(proc)
print(f" ✓ Killed PID {proc.pid} ({proc.name()})")
except:
pass
print("\n" + "=" * 80)
print(f"✓ Killed {len(self.killed)} processes")
print("=" * 80 + "\n")
return len(self.killed)
if __name__ == "__main__":
killer = AggressiveOrphanKiller()
killed = killer.run()
exit(0 if killed > 0 else 1)