Files
crypto_okx/deploy/pull_and_restart.sh
T
dekun ecf35f6c09 Fix quick-update PM2 path when crypto_okx was never started.
Stop trusting pm2 pid; start ecosystem if the app is missing.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-13 20:18:06 +08:00

69 lines
1.5 KiB
Bash

#!/usr/bin/env bash
# 服务器上拉代码并重启 PM2.
# 用法(/opt/crypto_okx 下 root):
# bash deploy/pull_and_restart.sh
# bash deploy/pull_and_restart.sh --dry-run
set -e
set -u
if [ -n "${BASH_VERSION:-}" ]; then
set -o pipefail
fi
REPO="${REPO:-/opt/crypto_okx}"
PM2_APP_NAME="${PM2_APP_NAME:-crypto_okx}"
DRY=0
if [[ "${1:-}" == "--dry-run" ]]; then
DRY=1
echo "(dry-run mode)"
fi
pm2_has_app() {
local name="$1"
if ! command -v pm2 >/dev/null 2>&1; then
return 1
fi
# 勿用 pm2 pid:部分版本对不存在进程仍 exit 0
if command -v python3 >/dev/null 2>&1; then
pm2 jlist 2>/dev/null | python3 -c '
import json,sys
name=sys.argv[1]
try:
apps=json.load(sys.stdin)
except Exception:
sys.exit(1)
sys.exit(0 if any(isinstance(a,dict) and a.get("name")==name for a in (apps or [])) else 1)
' "${name}"
return $?
fi
pm2 describe "${name}" 2>/dev/null | grep -qE 'status[[:space:]]*[|:]'
}
cd "${REPO}"
echo ">>> git pull"
git pull
if [[ "${DRY}" -eq 1 ]]; then
echo "(dry-run, skip pm2 restart)"
exit 0
fi
if ! command -v pm2 >/dev/null 2>&1; then
echo "warn: 未安装 pm2" >&2
exit 1
fi
if [[ ! -f ecosystem.config.cjs ]]; then
echo "error: 缺少 ecosystem.config.cjs" >&2
exit 1
fi
if pm2_has_app "${PM2_APP_NAME}"; then
echo ">>> pm2 restart ${PM2_APP_NAME} --update-env"
pm2 restart "${PM2_APP_NAME}" --update-env
else
echo ">>> 未注册 ${PM2_APP_NAME},执行 pm2 start ecosystem.config.cjs"
pm2 start ecosystem.config.cjs
fi
echo "done"