Repository navigation
Expand file tree
/
Copy pathstop.ps1
More file actions
156 lines (133 loc) · 5.75 KB
/
Copy pathstop.ps1
File metadata and controls
156 lines (133 loc) · 5.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
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
156
# ═══════════════════════════════════════════════════════════════════════════════
# KNIGHTBOT CANONICAL SHUTDOWN SCRIPT
# ═══════════════════════════════════════════════════════════════════════════════
$ErrorActionPreference = "Continue"
$K = "F:\KnightBot"
function Get-ListeningConnections {
param([Parameter(Mandatory=$true)][int[]]$Ports)
$all = @()
foreach ($p in $Ports) {
$all += Get-NetTCPConnection -State Listen -LocalPort $p -ErrorAction SilentlyContinue
}
return @($all | Where-Object { $_ -and $_.OwningProcess })
}
function Format-ListenerSummary {
param([Parameter(Mandatory=$true)]$Connections)
if (-not $Connections -or $Connections.Count -eq 0) { return "(none)" }
$pids = @($Connections | Select-Object -ExpandProperty OwningProcess -Unique)
$procs = Get-Process -Id $pids -ErrorAction SilentlyContinue |
Select-Object Id, ProcessName
return ($Connections |
Select-Object LocalPort, OwningProcess |
Sort-Object LocalPort, OwningProcess |
Format-Table -AutoSize | Out-String) + "`n" + ($procs | Format-Table -AutoSize | Out-String)
}
function Stop-ProcessHard {
param(
[Parameter(Mandatory=$true)][int]$ProcessId,
[string]$Reason = ""
)
try {
Stop-Process -Id $ProcessId -Force -ErrorAction Stop
Write-Host " OK: Stop-Process killed PID $ProcessId $Reason" -ForegroundColor DarkGray
return $true
}
catch {
Write-Host " WARN: Stop-Process failed for PID $ProcessId ${Reason}: $($_.Exception.Message)" -ForegroundColor Yellow
try {
# Fallback: taskkill can sometimes terminate stubborn process trees.
& taskkill.exe /PID $ProcessId /T /F | Out-Null
Write-Host " OK: taskkill killed PID $ProcessId $Reason" -ForegroundColor DarkGray
return $true
}
catch {
Write-Host " ERROR: taskkill failed for PID $ProcessId ${Reason}: $($_.Exception.Message)" -ForegroundColor Red
return $false
}
}
}
function Stop-ProcessesByCommandPattern {
param(
[Parameter(Mandatory=$true)][string]$Pattern,
[Parameter(Mandatory=$true)][string]$Label
)
try {
$targets = Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {
$_.Name -match '^python(w)?\.exe$' -and
$_.CommandLine -and
$_.CommandLine -match $Pattern
}
}
catch {
$targets = @()
}
if (-not $targets -or $targets.Count -eq 0) { return }
Write-Host ("Found " + $targets.Count + " extra " + $Label + " process(es) by command line; stopping...") -ForegroundColor DarkGray
foreach ($t in $targets) {
$null = Stop-ProcessHard -ProcessId $t.ProcessId -Reason "($Label commandline)"
}
}
function Invoke-DockerCompose {
param(
[Parameter(Mandatory=$true)][string[]]$Args
)
# Prefer the modern plugin (`docker compose`). If user has legacy `docker-compose`, fall back.
try {
$null = docker compose version 2>$null
& docker compose @Args
return $LASTEXITCODE
}
catch {
& docker-compose @Args
return $LASTEXITCODE
}
}
Write-Host "`nStopping KnightBot...`n" -ForegroundColor Yellow
# Stop duplicate/non-listening Knight Core workers by commandline signature.
Stop-ProcessesByCommandPattern -Pattern 'scripts[\\/]knight_core\.py' -Label "Knight Core"
# Stop services by known listening ports (safer than killing all python/node)
$ports = @(3000, 8060, 8070, 8071, 8100, 7880, 7881)
$before = Get-ListeningConnections -Ports $ports
if ($before.Count -gt 0) {
Write-Host "Listeners before stop:" -ForegroundColor DarkGray
Write-Host (Format-ListenerSummary -Connections $before) -ForegroundColor DarkGray
}
# Retry loop: some processes briefly re-bind or take a moment to terminate.
$deadline = (Get-Date).AddSeconds(12)
do {
$connections = Get-ListeningConnections -Ports $ports
foreach ($conn in $connections) {
$processId = $conn.OwningProcess
$port = $conn.LocalPort
if ($processId) {
$null = Stop-ProcessHard -ProcessId $processId -Reason "(port $port)"
}
}
Start-Sleep -Milliseconds 750
$still = Get-ListeningConnections -Ports $ports
} while ($still.Count -gt 0 -and (Get-Date) -lt $deadline)
if ($still.Count -gt 0) {
Write-Host "WARNING: Some listeners are still active after stop attempts:" -ForegroundColor Yellow
Write-Host (Format-ListenerSummary -Connections $still) -ForegroundColor Yellow
}
# Best-effort: sometimes Node/FastAPI keep a socket open briefly after Stop-Process.
Start-Sleep -Seconds 2
Set-Location $K
Invoke-DockerCompose -Args @("down") 2>$null
# Also stop common non-compose containers that can block required ports.
# This repo historically started some containers manually (outside docker compose),
# e.g. a container literally named "Qdrant".
function Stop-DockerContainerIfRunning {
param([Parameter(Mandatory=$true)][string]$Name)
try {
$running = docker ps --format "{{.Names}}" | Where-Object { $_ -eq $Name }
if ($running) {
docker stop $Name | Out-Null
Write-Host " OK: Stopped Docker container '$Name'" -ForegroundColor DarkGray
}
} catch {}
}
Stop-DockerContainerIfRunning -Name "Qdrant"
Stop-DockerContainerIfRunning -Name "mem0"
Stop-DockerContainerIfRunning -Name "openmemory-mcp"
Write-Host "`nOK: KnightBot stopped`n" -ForegroundColor Green