Files
sgame-admin-deploy/app-entry.sh
2026-06-11 18:25:25 +08:00

46 lines
1.4 KiB
Bash
Raw 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/sh
# sgame-admin-server 容器守护入口alpine 容器内由 busybox sh 执行)。
# /repo 是 gitsync 容器同步下来的部署仓(只读挂载),里面有:
# server — linux/amd64 静态二进制(前端已 embed时区已内嵌
# config.yaml — 基础配置(敏感项全部由环境变量覆盖)
# app-entry.sh — 本脚本
# 逻辑:启动 server → 每 10s 比对仓库里二进制的 md5 → 变了就杀进程重启。
# 进程崩溃同样会回到循环顶部自动拉起。
BIN=/repo/server
RUN=/tmp/server.run
echo "[entry] waiting for $BIN ..."
while [ ! -f "$BIN" ]; do sleep 2; done
while true; do
# 等 gitsync 的 checkout 落定:两次校验和一致才启动
SUM=$(md5sum "$BIN" | cut -d' ' -f1)
sleep 1
SUM2=$(md5sum "$BIN" | cut -d' ' -f1)
if [ "$SUM" != "$SUM2" ]; then
continue
fi
# 跑 /tmp 副本而不是 /repo 原件,避免 git reset 覆盖运行中的可执行文件
cp "$BIN" "$RUN"
chmod +x "$RUN"
echo "[entry] starting server (md5 $SUM)"
"$RUN" -config /repo/config.yaml &
PID=$!
while kill -0 "$PID" 2>/dev/null; do
sleep 10
NEW=$(md5sum "$BIN" 2>/dev/null | cut -d' ' -f1)
if [ -n "$NEW" ] && [ "$NEW" != "$SUM" ]; then
echo "[entry] new binary detected, restarting"
kill "$PID"
wait "$PID" 2>/dev/null
break
fi
done
echo "[entry] server exited, relaunching in 2s"
sleep 2
done