Files
crypto_monitor/manual_trading_hub/scripts/pm2_hub.sh
T

92 lines
1.9 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.
#!/usr/bin/env bash
# 中控 + 子代理 统一 PM2 快捷脚本
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HUB_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
ECO="${HUB_DIR}/ecosystem.config.cjs"
# 与 ecosystem.config.cjs 中 name 一致
PM2_NAMES=(
manual-agent-binance
manual-agent-okx
manual-agent-gate
manual-agent-gate-bot
manual-trading-hub
)
usage() {
cat <<'EOF'
用法: bash scripts/pm2_hub.sh <start|stop|restart|status|logs|delete>
start 启动 ecosystem.config.cjs4 路子代理 + 中控,已存在则 restart 全部)
stop 停止全部
restart 重启全部
status pm2 status
logs 全部相关进程日志
delete 从 PM2 列表移除全部
仅中控: pm2 start ecosystem.config.cjs --only manual-trading-hub
EOF
}
cmd="${1:-}"
if ! command -v pm2 >/dev/null 2>&1; then
echo "未找到 pm2,请先: npm install -g pm2" >&2
exit 1
fi
if [[ ! -f "${ECO}" ]]; then
echo "未找到 ${ECO}" >&2
exit 1
fi
cd "${HUB_DIR}"
_any_running() {
local n
for n in "${PM2_NAMES[@]}"; do
if pm2 describe "${n}" >/dev/null 2>&1; then
return 0
fi
done
return 1
}
case "${cmd}" in
start)
if _any_running; then
pm2 restart "${ECO}"
echo "已重启:hub + 全部 agent"
else
pm2 start "${ECO}"
echo "已启动:hub + 全部 agent(共 ${#PM2_NAMES[@]} 个进程)"
fi
pm2 save 2>/dev/null || true
;;
stop)
pm2 stop "${PM2_NAMES[@]}" 2>/dev/null || echo "部分或全部进程未在运行"
;;
restart)
if _any_running; then
pm2 restart "${ECO}"
else
pm2 start "${ECO}"
fi
;;
status)
pm2 status
;;
logs)
pm2 logs "${PM2_NAMES[@]}" --lines 100
;;
delete)
pm2 delete "${PM2_NAMES[@]}" 2>/dev/null || echo "部分或全部进程不存在"
;;
*)
usage
exit 1
;;
esac