修复端口号重复的问题
This commit is contained in:
parent
ec563f8331
commit
8a731779ac
79
jsite.sh
79
jsite.sh
@ -616,7 +616,8 @@ install_pm2() {
|
||||
|
||||
# 设置PM2开机自启
|
||||
log_info "配置PM2开机自启..."
|
||||
sudo env PATH="$PATH:/home/jingrow/.nvm/versions/node/v22.18.0/bin" pm2 startup systemd -u jingrow --hp /home/jingrow --service-name pm2-jingrow --silent
|
||||
local node_path=$(ls -d /home/jingrow/.nvm/versions/node/v* 2>/dev/null | head -1)
|
||||
sudo env PATH="$PATH:$node_path/bin" pm2 startup systemd -u jingrow --hp /home/jingrow --service-name pm2-jingrow --silent
|
||||
log_success "PM2开机自启配置完成"
|
||||
}
|
||||
|
||||
@ -666,21 +667,39 @@ get_available_port() {
|
||||
user_specified_port=true
|
||||
fi
|
||||
|
||||
if [ "$user_specified_port" = true ]; then
|
||||
# 用户传入了自定义端口,优先使用用户指定的端口
|
||||
# 检查用户指定的端口是否已被使用
|
||||
local port_available=true
|
||||
if [ -f "$port_file" ]; then
|
||||
# 确保jq工具已安装
|
||||
ensure_jq_installed
|
||||
|
||||
if command -v jq &> /dev/null; then
|
||||
local used_ports=$(jq -r '.[]' "$port_file" 2>/dev/null || echo "")
|
||||
# 检查端口文件是否有效(包含有效的端口分配)
|
||||
local port_file_valid=false
|
||||
if [ -f "$port_file" ]; then
|
||||
# 确保jq工具已安装
|
||||
ensure_jq_installed
|
||||
|
||||
if command -v jq &> /dev/null; then
|
||||
# 使用jq检查文件是否包含有效的端口分配
|
||||
local valid_ports=$(jq -r '.[] | select(type == "number" and . > 0)' "$port_file" 2>/dev/null || echo "")
|
||||
if [ -n "$valid_ports" ]; then
|
||||
port_file_valid=true
|
||||
fi
|
||||
else
|
||||
# 使用grep检查文件是否包含有效的端口分配
|
||||
local valid_ports=$(grep -o '"[^"]*"[[:space:]]*:[[:space:]]*[0-9]\+' "$port_file" 2>/dev/null | cut -d: -f2 | tr -d ' ' | grep -E '^[0-9]+$' || echo "")
|
||||
if [ -n "$valid_ports" ]; then
|
||||
port_file_valid=true
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$user_specified_port" = true ]; then
|
||||
# 用户传入了自定义端口,优先使用用户指定的端口
|
||||
# 检查用户指定的端口是否已被使用
|
||||
local port_available=true
|
||||
if [ "$port_file_valid" = true ]; then
|
||||
if command -v jq &> /dev/null; then
|
||||
local used_ports=$(jq -r '.[] | select(type == "number" and . > 0)' "$port_file" 2>/dev/null || echo "")
|
||||
if echo "$used_ports" | grep -q "^$base_port$"; then
|
||||
port_available=false
|
||||
fi
|
||||
else
|
||||
local used_ports=$(grep -o ":[[:space:]]*[0-9]*" "$port_file" 2>/dev/null | cut -d: -f2 | tr -d ' ' || echo "")
|
||||
local used_ports=$(grep -o '"[^"]*"[[:space:]]*:[[:space:]]*[0-9]\+' "$port_file" 2>/dev/null | cut -d: -f2 | tr -d ' ' || echo "")
|
||||
if echo "$used_ports" | grep -q "^$base_port$"; then
|
||||
port_available=false
|
||||
fi
|
||||
@ -692,17 +711,14 @@ get_available_port() {
|
||||
next_port="$base_port"
|
||||
else
|
||||
# 用户指定的端口已被使用,使用最大端口号 + 1
|
||||
if [ -f "$port_file" ]; then
|
||||
# 确保jq工具已安装
|
||||
ensure_jq_installed
|
||||
|
||||
if [ "$port_file_valid" = true ]; then
|
||||
if command -v jq &> /dev/null; then
|
||||
local max_port=$(jq -r 'max(.[])' "$port_file" 2>/dev/null || echo "$base_port")
|
||||
local max_port=$(jq -r 'max(.[] | select(type == "number" and . > 0))' "$port_file" 2>/dev/null || echo "$base_port")
|
||||
if [ -n "$max_port" ] && [ "$max_port" != "null" ]; then
|
||||
next_port=$((max_port + increment))
|
||||
fi
|
||||
else
|
||||
local max_port=$(grep -o ":[[:space:]]*[0-9]*" "$port_file" 2>/dev/null | cut -d: -f2 | tr -d ' ' | sort -n | tail -1 || echo "$base_port")
|
||||
local max_port=$(grep -o '"[^"]*"[[:space:]]*:[[:space:]]*[0-9]\+' "$port_file" 2>/dev/null | cut -d: -f2 | tr -d ' ' | sort -n | tail -1 || echo "$base_port")
|
||||
if [ -n "$max_port" ]; then
|
||||
next_port=$((max_port + increment))
|
||||
fi
|
||||
@ -711,19 +727,16 @@ get_available_port() {
|
||||
fi
|
||||
else
|
||||
# 用户没有传入自定义端口,使用自动分配逻辑
|
||||
if [ -f "$port_file" ]; then
|
||||
# 确保jq工具已安装
|
||||
ensure_jq_installed
|
||||
|
||||
if [ "$port_file_valid" = true ]; then
|
||||
if command -v jq &> /dev/null; then
|
||||
# 使用jq找到最大端口号
|
||||
local max_port=$(jq -r 'max(.[])' "$port_file" 2>/dev/null || echo "$base_port")
|
||||
local max_port=$(jq -r 'max(.[] | select(type == "number" and . > 0))' "$port_file" 2>/dev/null || echo "$base_port")
|
||||
if [ -n "$max_port" ] && [ "$max_port" != "null" ]; then
|
||||
next_port=$((max_port + increment))
|
||||
fi
|
||||
else
|
||||
# 使用grep和sort找到最大端口号
|
||||
local max_port=$(grep -o ":[[:space:]]*[0-9]*" "$port_file" 2>/dev/null | cut -d: -f2 | tr -d ' ' | sort -n | tail -1 || echo "$base_port")
|
||||
local max_port=$(grep -o '"[^"]*"[[:space:]]*:[[:space:]]*[0-9]\+' "$port_file" 2>/dev/null | cut -d: -f2 | tr -d ' ' | sort -n | tail -1 || echo "$base_port")
|
||||
if [ -n "$max_port" ]; then
|
||||
next_port=$((max_port + increment))
|
||||
fi
|
||||
@ -731,6 +744,21 @@ get_available_port() {
|
||||
fi
|
||||
fi
|
||||
|
||||
# 简单修复:确保分配的端口不重复
|
||||
if [ "$port_file_valid" = true ]; then
|
||||
local used_ports=""
|
||||
if command -v jq &> /dev/null; then
|
||||
used_ports=$(jq -r '.[] | select(type == "number" and . > 0)' "$port_file" 2>/dev/null || echo "")
|
||||
else
|
||||
used_ports=$(grep -o '"[^"]*"[[:space:]]*:[[:space:]]*[0-9]\+' "$port_file" 2>/dev/null | cut -d: -f2 | tr -d ' ' || echo "")
|
||||
fi
|
||||
|
||||
# 如果端口已被使用,递增直到找到可用端口
|
||||
while echo "$used_ports" | grep -q "^$next_port$"; do
|
||||
next_port=$((next_port + increment))
|
||||
done
|
||||
fi
|
||||
|
||||
echo "$next_port"
|
||||
return 0
|
||||
}
|
||||
@ -2188,7 +2216,8 @@ autostartup_site() {
|
||||
|
||||
# 保存PM2进程列表
|
||||
log_info "保存PM2进程列表..."
|
||||
sudo env PATH="$PATH:/home/jingrow/.nvm/versions/node/v22.18.0/bin" su - jingrow -c "pm2 save --silent"
|
||||
local node_path=$(ls -d /home/jingrow/.nvm/versions/node/v* 2>/dev/null | head -1)
|
||||
sudo env PATH="$PATH:$node_path/bin" su - jingrow -c "pm2 save --silent"
|
||||
log_success "PM2进程列表保存完成"
|
||||
|
||||
log_success "网站 $SITE_NAME 自动启动配置完成"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user