zhangjiaqing
1 天以前 350fd63763da0d3a2ca9a7107dc30c6a007a7b70
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
# Kill process using port 3000
$port = 3000
 
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Port ${port} Cleanup Script" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan
 
# Method 1: Check using Get-NetTCPConnection
Write-Host "Method 1: Checking with Get-NetTCPConnection..." -ForegroundColor Yellow
$connections = Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue
 
if ($connections) {
    Write-Host "Found processes listening on port ${port}:" -ForegroundColor Green
    $connections | ForEach-Object {
        $pid = $_.OwningProcess
        $process = Get-Process -Id $pid -ErrorAction SilentlyContinue
        
        if ($process) {
            Write-Host "  - PID: $pid, Name: $($process.ProcessName), Path: $($process.Path)" -ForegroundColor White
            Write-Host "    Killing process..." -ForegroundColor Yellow
            try {
                Stop-Process -Id $pid -Force
                Write-Host "    ✓ Successfully killed PID $pid" -ForegroundColor Green
            } catch {
                Write-Host "    ✗ Failed to kill PID $pid : $_" -ForegroundColor Red
            }
        }
    }
} else {
    Write-Host "No processes found listening on port ${port}" -ForegroundColor Yellow
}
 
# Method 2: Check using netstat (more reliable on some systems)
Write-Host "`nMethod 2: Checking with netstat..." -ForegroundColor Yellow
$portPattern = ":${port}" + ".*LISTENING"
$netstatLines = netstat -ano | Select-String $portPattern
 
if ($netstatLines) {
    Write-Host "Found processes via netstat:" -ForegroundColor Green
    $netstatLines | ForEach-Object {
        if ($_.Line -match '\s+(\d+)\s*$') {
            $pid = $matches[1]
            $process = Get-Process -Id $pid -ErrorAction SilentlyContinue
            
            if ($process) {
                Write-Host "  - PID: $pid, Name: $($process.ProcessName)" -ForegroundColor White
                Write-Host "    Killing process..." -ForegroundColor Yellow
                try {
                    Stop-Process -Id $pid -Force
                    Write-Host "    ✓ Successfully killed PID $pid" -ForegroundColor Green
                } catch {
                    Write-Host "    ✗ Failed to kill PID $pid : $_" -ForegroundColor Red
                }
            }
        }
    }
} else {
    Write-Host "No processes found via netstat" -ForegroundColor Yellow
}
 
# Final check
Write-Host "`nFinal check..." -ForegroundColor Yellow
Start-Sleep -Seconds 1
$finalCheck = Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue
 
if ($finalCheck) {
    Write-Host "Warning: Port ${port} is still in use!" -ForegroundColor Red
    Write-Host "You may need to:" -ForegroundColor Yellow
    Write-Host "  1. Run this script as Administrator" -ForegroundColor White
    Write-Host "  2. Manually kill the process using Task Manager" -ForegroundColor White
    Write-Host "  3. Change the port in bin/www or set PORT environment variable" -ForegroundColor White
} else {
    Write-Host "Port ${port} is now free!" -ForegroundColor Green
}
 
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "You can now run: npm run dev" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan