Files
2025-10-07 15:58:15 +08:00

28 lines
750 B
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#
# 检查本地端口是否在指定超时时间内变为可用
# 用法: ./check_port.sh <port> [timeout]
# 例如: ./check_port.sh 8000 60
#
PORT=${1:-8000}
TIMEOUT=${2:-60}
INTERVAL=5
START_TIME=$(date +%s)
END_TIME=$((START_TIME + TIMEOUT))
echo "开始检测: 端口 $PORT,超时时间 ${TIMEOUT}s每次间隔 ${INTERVAL}s"
while [ $(date +%s) -lt $END_TIME ]; do
if nc -z -w1 localhost "$PORT" 2>/dev/null; then
echo "[$(date '+%F %T')] ✅ 端口 $PORT 已启用"
exit 0
else
echo "[$(date '+%F %T')] ⏳ 端口 $PORT 尚为启动用,等待 ${INTERVAL}s..."
sleep $INTERVAL
fi
done
echo "[$(date '+%F %T')] ❌ 超时: 端口 $PORT${TIMEOUT}s 内未变为启用"
exit 1