Repository navigation
Expand file tree
/
Copy pathport_utils.sh
More file actions
78 lines (68 loc) · 2.23 KB
/
Copy pathport_utils.sh
File metadata and controls
78 lines (68 loc) · 2.23 KB
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
#!/bin/bash
# Utility function to find an available port
# Requires: used_ports array to be declared in the sourcing script before sourcing this file
# Usage: port=$(find_available_port $base_port)
find_available_port() {
local base_port=$1
local port=$base_port
# First check our internal tracking
while [[ " ${used_ports[@]} " =~ " ${port} " ]]; do
port=$((port + 1))
done
# Then check system ports if tools are available
if command -v netstat >/dev/null 2>&1; then
while netstat -tuln 2>/dev/null | grep -q ":$port "; do
port=$((port + 1))
done
elif command -v ss >/dev/null 2>&1; then
while ss -tuln 2>/dev/null | grep -q ":$port "; do
port=$((port + 1))
done
elif command -v lsof >/dev/null 2>&1; then
while lsof -i :$port >/dev/null 2>&1; do
port=$((port + 1))
done
else
# Fallback: just increment port and hope for the best
echo "⚠️ Warning: No port checking tools available, using port ${port}" >&2
fi
# Add to our tracking
used_ports+=($port)
echo $port
}
# Define a function to check if server is ready
wait_for_server() {
local port=$1
local max_attempts=30
local attempt=0
echo "⏳ Waiting for server on port ${port} to be ready..." >&2
while [ $attempt -lt $max_attempts ]; do
# Try different methods to check if port is in use
local port_in_use=false
# Method 1: Try to connect to the port (most reliable)
if timeout 1 bash -c "echo >/dev/tcp/localhost/$port" 2>/dev/null; then
port_in_use=true
# Method 2: Use system tools if available
elif command -v netstat >/dev/null 2>&1; then
if netstat -tuln 2>/dev/null | grep -q ":$port "; then
port_in_use=true
fi
elif command -v ss >/dev/null 2>&1; then
if ss -tuln 2>/dev/null | grep -q ":$port "; then
port_in_use=true
fi
elif command -v lsof >/dev/null 2>&1; then
if lsof -i :$port >/dev/null 2>&1; then
port_in_use=true
fi
fi
if [ "$port_in_use" = true ]; then
echo "✅ Server on port ${port} is ready" >&2
return 0
fi
sleep 2
attempt=$((attempt + 1))
done
echo "❌ Server on port ${port} failed to start after $((max_attempts * 2)) seconds" >&2
return 1
}