64 lines
1.5 KiB
Bash
Executable File
64 lines
1.5 KiB
Bash
Executable File
#!/bin/bash
|
||
set -Eeuo pipefail
|
||
|
||
PORT=5173
|
||
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
|
||
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
|
||
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
|
||
}
|
||
|
||
kill_port_if_listening
|
||
|
||
echo "Starting HTTP service on port ${PORT} for dev..."
|
||
|
||
pnpm dev -- --host 0.0.0.0 --port $PORT |