# 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
|