- Add start_aiagent_background.ps1 for silent background startup - Add install_autostart.ps1 for Windows Task Scheduler registration - Add 项目初始文档.md as quick reference for project onboarding - Update stop_aiagent.ps1 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
66 lines
1.9 KiB
PowerShell
66 lines
1.9 KiB
PowerShell
$ErrorActionPreference = "SilentlyContinue"
|
||
|
||
Write-Host "== AIAgent stop ==" -ForegroundColor Cyan
|
||
|
||
function Get-PidsListeningOnPort([int]$Port) {
|
||
$pids = New-Object System.Collections.Generic.HashSet[int]
|
||
try {
|
||
netstat -ano | ForEach-Object {
|
||
$ln = $_.Trim()
|
||
if ($ln -notmatch "LISTENING") { return }
|
||
# 匹配 :8037 或 :3001 等端口后的 LISTENING 行末 PID
|
||
if ($ln -match ":$Port\s+.*LISTENING\s+(\d+)\s*$") {
|
||
[void]$pids.Add([int]$Matches[1])
|
||
}
|
||
}
|
||
} catch { }
|
||
return @($pids)
|
||
}
|
||
|
||
function Stop-OnPorts([int[]]$ports, [string]$name) {
|
||
$all = New-Object System.Collections.Generic.HashSet[int]
|
||
foreach ($p in $ports) {
|
||
foreach ($pid in (Get-PidsListeningOnPort $p)) {
|
||
[void]$all.Add($pid)
|
||
}
|
||
}
|
||
if ($all.Count -eq 0) {
|
||
Write-Host "[SKIP] ${name}: no listener on ports $($ports -join ',')" -ForegroundColor DarkGray
|
||
return
|
||
}
|
||
foreach ($pid in $all) {
|
||
if ($pid -le 4) { continue }
|
||
try {
|
||
Stop-Process -Id $pid -Force -ErrorAction SilentlyContinue
|
||
Write-Host "[OK] stopped ${name} PID=$pid" -ForegroundColor Green
|
||
} catch {
|
||
Write-Host "[WARN] failed to stop ${name} PID=$pid" -ForegroundColor Yellow
|
||
}
|
||
}
|
||
}
|
||
|
||
# 后端 API(8037 / 8041 备用)
|
||
Stop-OnPorts @(8037, 8041) "backend-api"
|
||
|
||
# 前端 Vite(3001)
|
||
Stop-OnPorts @(3001) "frontend-dev"
|
||
|
||
# Redis(6379,仅当监听在本地开发端口时结束;若与其它项目共用请谨慎)
|
||
Stop-OnPorts @(6379) "redis"
|
||
|
||
Start-Sleep -Milliseconds 600
|
||
|
||
Write-Host ""
|
||
Write-Host "Port check:" -ForegroundColor Cyan
|
||
foreach ($port in 3001, 8037, 8041, 6379) {
|
||
$line = netstat -ano | Select-String ":$port\s+.*LISTENING" | Select-Object -First 1
|
||
if ($line) {
|
||
Write-Host " - ${port}: LISTEN" -ForegroundColor Yellow
|
||
} else {
|
||
Write-Host " - ${port}: free" -ForegroundColor Green
|
||
}
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "DONE: stop script finished" -ForegroundColor Green
|