$projectDir = "E:\ZG_Dev\ZhuiGuangAI\zhuiguang-ai" $defaultPort = 8301 Write-Host "========================================" -ForegroundColor Cyan Write-Host " 追光AI 服务重启脚本" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "`n[1/3] 扫描项目端口 (8301-8310)..." -ForegroundColor Yellow $foundPids = @() foreach ($port in 8301..8310) { $conn = Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue if ($conn) { foreach ($c in $conn) { if ($c.OwningProcess -gt 0 -and $foundPids -notcontains $c.OwningProcess) { $proc = Get-Process -Id $c.OwningProcess -ErrorAction SilentlyContinue if ($proc) { $foundPids += $c.OwningProcess Write-Host " 发现进程 PID=$($c.OwningProcess) ($($proc.ProcessName)) 监听端口 $port" -ForegroundColor Red } } } } } if ($foundPids.Count -gt 0) { Write-Host "`n[2/3] 关闭已有服务..." -ForegroundColor Yellow foreach ($procId in $foundPids) { try { Stop-Process -Id $procId -Force -ErrorAction Stop Write-Host " 已终止 PID=$procId" -ForegroundColor Green } catch { Write-Host " 终止失败 PID=$procId : $_" -ForegroundColor Red } } Start-Sleep -Seconds 1 } else { Write-Host "`n[2/3] 无需关闭,未检测到运行中的服务" -ForegroundColor Green } Write-Host "`n[3/3] 启动服务 (端口 $defaultPort)..." -ForegroundColor Yellow Start-Process -FilePath "cmd.exe" -ArgumentList "/c", "npm run dev" -WorkingDirectory $projectDir Start-Sleep -Seconds 4 $check = Get-NetTCPConnection -LocalPort $defaultPort -State Listen -ErrorAction SilentlyContinue if ($check) { Write-Host "`n========================================" -ForegroundColor Green Write-Host " 服务已启动: http://localhost:$defaultPort" -ForegroundColor Green Write-Host "========================================" -ForegroundColor Green } else { Write-Host "`n 服务启动中,请稍后访问 http://localhost:$defaultPort" -ForegroundColor Yellow }