更新 scripts/start.sh
This commit is contained in:
@@ -7,58 +7,17 @@ WORKSPACE_PATH="/workspace"
|
||||
cd $WORKSPACE_PATH
|
||||
|
||||
kill_port_if_listening() {
|
||||
local port="$PORT"
|
||||
local hex_port
|
||||
local inodes=""
|
||||
local pids=""
|
||||
|
||||
# 转16进制端口(大端)
|
||||
hex_port=$(printf '%04X\n' "$port")
|
||||
|
||||
# 找 TCP (IPv4)
|
||||
inodes+=$(awk -v port=":$hex_port" '$2 ~ port && $4 == "0A" {print $10}' /proc/net/tcp 2>/dev/null || true)
|
||||
|
||||
# 找 TCP (IPv6)
|
||||
inodes+=" "
|
||||
inodes+=$(awk -v port=":$hex_port" '$2 ~ port && $4 == "0A" {print $10}' /proc/net/tcp6 2>/dev/null || true)
|
||||
|
||||
inodes=$(echo "$inodes" | tr ' ' '\n' | sort -u | tr '\n' ' ')
|
||||
|
||||
if [[ -z "$inodes" ]]; then
|
||||
return 0
|
||||
local pids
|
||||
pids=$(ss -H -lntp 2>/dev/null | awk -v port="${PORT}" '$4 ~ ":"port"$"' | grep -o 'pid=[0-9]*' | cut -d= -f2 | paste -sd' ' - || true)
|
||||
if [[ -z "${pids}" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
# 通过 inode 找 PID
|
||||
for inode in $inodes; do
|
||||
for pid in $(ls /proc | grep -E '^[0-9]+$'); do
|
||||
if ls -l /proc/$pid/fd 2>/dev/null | grep -q "socket:\[$inode\]"; then
|
||||
pids="$pids $pid"
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
# 去重
|
||||
pids=$(echo "$pids" | tr ' ' '\n' | sort -u | tr '\n' ' ')
|
||||
pids="${pids#" "}"
|
||||
|
||||
if [[ -z "$pids" ]]; then
|
||||
echo "Port ${port} has socket but no PID found."
|
||||
return 1
|
||||
fi
|
||||
|
||||
kill -9 $pids 2>/dev/null || true
|
||||
echo "${pids}" | xargs -I {} kill -9 {}
|
||||
sleep 1
|
||||
|
||||
local check
|
||||
check=$(awk -v port=":$hex_port" '$2 ~ port && $4 == "0A"' /proc/net/tcp /proc/net/tcp6 2>/dev/null || true)
|
||||
|
||||
if [[ -n "$check" ]]; then
|
||||
echo "Warning: port ${port} still busy after SIGKILL"
|
||||
fi
|
||||
pids=$(ss -H -lntp 2>/dev/null | awk -v port="${PORT}" '$4 ~ ":"port"$"' | grep -o 'pid=[0-9]*' | cut -d= -f2 | paste -sd' ' - || true)
|
||||
}
|
||||
|
||||
kill_port_if_listening
|
||||
|
||||
echo "Starting HTTP service on port ${PORT} for dev..."
|
||||
|
||||
pnpm dev -- --host 0.0.0.0 --port $PORT
|
||||
pnpm dev -- --host 0.0.0.0 --port $PORT
|
||||
|
||||
Reference in New Issue
Block a user