Files
group_xinghuo_jinrong/scripts/dev/stop-backend.ps1
T

37 lines
1.4 KiB
PowerShell
Raw Normal View History

#Requires -Version 5.1
<#
.SYNOPSIS
停止占用 8000 的 JinRong uvicorn(及非 jinrong Docker 容器)。
#>
$ErrorActionPreference = "Stop"
function Get-ListenerPids([int[]]$Ports) {
$pids = @()
foreach ($port in $Ports) {
Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue |
ForEach-Object { $pids += $_.OwningProcess }
}
$pids | Select-Object -Unique
}
Write-Host "Stopping uvicorn app.main on :8000 ..."
Get-CimInstance Win32_Process -Filter "Name='python.exe'" -ErrorAction SilentlyContinue |
Where-Object { $_.CommandLine -match 'uvicorn' -and $_.CommandLine -match 'app\.main:app' -and $_.CommandLine -match ':8000' } |
ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
foreach ($procId in (Get-ListenerPids @(8000))) {
Stop-Process -Id $procId -Force -ErrorAction SilentlyContinue
}
if (Get-Command docker -ErrorAction SilentlyContinue) {
$name = docker ps --filter "publish=8000" --format "{{.Names}}" 2>$null | Select-Object -First 1
if ($name -and ($name.Trim() -notmatch '^jinrong')) {
Write-Host "Stopping Docker $name on 8000 ..."
docker stop $name.Trim() | Out-Null
}
}
Start-Sleep -Seconds 2
netstat -ano | findstr ":8000.*LISTENING"
Write-Host "Done. Start backend: .\scripts\dev\restart-dev.ps1 -Backend Local -SkipFrontend"