Files
hyperf-micro-svc/restart_prod.sh

46 lines
1.5 KiB
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
HomeDir=/home/production/cdiscount/server
# 替 .env
rm -rf "$HomeDir/.env"
cp "$HomeDir/.env.prod" "$HomeDir/.env"
env_file="$HomeDir/.env"
# 设置 CRONTAB_ENABLE
if [ -e "$HomeDir/../cron_true.txt" ]; then
sed -i 's/^CRONTAB_ENABLE=.*/CRONTAB_ENABLE=true/' "$env_file"
else
sed -i 's/^CRONTAB_ENABLE=.*/CRONTAB_ENABLE=false/' "$env_file"
fi
# 从 server.php 中提取所有 'port' => 数字 的值(使用 grep + awk
# 匹配类似:'port' => 9501,
ports=$(grep -o "'port'[[:space:]]*=>[[:space:]]*[0-9]\+" "$HomeDir/config/autoload/server.php" | awk -F '=>' '{print $2}' | tr -d ' ,')
# 转为数组
read -a port_array <<< "$ports"
if [ ${#port_array[@]} -eq 0 ]; then
echo "Error: No valid ports found in server.php. Exiting."
exit 1
fi
echo "Detected ports: ${port_array[*]}"
# 只 kill 正在被监听的端口
for port in "${port_array[@]}"; do
if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
echo "Warning: Skipping invalid port '$port'"
continue
fi
if ss -tuln | grep -q ":$port\b"; then
echo "Port $port is in use. Killing related processes..."
sudo lsof -i:"$port" -t | sudo xargs -r kill -9
else
echo "Port $port is not in use. Skip killing."
fi
done
echo 'Cleanup done.'
# 启动服务
source /etc/profile
mkdir -p /home/log
nohup php "$HomeDir/bin/hyperf.php" start > /home/log/hyperf_run.log 2>&1 &
echo 'Service restarted successfully on ports: '"${port_array[*]}"' '