Stop uninstall from killing unrelated PM2 processes on the server.

Only stop and delete crypto_monitor app names (plus legacy aliases) instead of pm2 stop/delete all.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-12 12:56:08 +08:00
parent 337a6bc54f
commit 8e492987ec
3 changed files with 60 additions and 11 deletions
+48
View File
@@ -27,6 +27,13 @@ PM2_APPS=(
manual-agent-gate manual-agent-gate
) )
# 历史进程名(仅 stop/delete 时兼容,不计入验收)
PM2_APPS_LEGACY=(
crypto-monitor-binance
crypto-monitor-gate
crypto-monitor-okx
)
CONFIG_PATHS=( CONFIG_PATHS=(
crypto_monitor_binance/.env crypto_monitor_binance/.env
crypto_monitor_okx/.env crypto_monitor_okx/.env
@@ -255,6 +262,47 @@ remove_backup_cron_all() {
fi fi
} }
pm2_app_exists() {
local name="$1"
pm2 pid "${name}" >/dev/null 2>&1
}
# 仅操作本项目 PM2 进程,不影响服务器上其它应用
pm2_stop_project_apps() {
if ! command -v pm2 >/dev/null 2>&1; then
log "未安装 pm2,跳过"
return 0
fi
local name stopped=0
for name in "${PM2_APPS[@]}" "${PM2_APPS_LEGACY[@]}"; do
if pm2_app_exists "${name}"; then
log "pm2 stop ${name}"
pm2 stop "${name}" 2>/dev/null || true
stopped=$((stopped + 1))
fi
done
if [[ "${stopped}" -eq 0 ]]; then
log "未发现本项目 PM2 进程(其它 PM2 不受影响)"
fi
}
pm2_delete_project_apps() {
if ! command -v pm2 >/dev/null 2>&1; then
return 0
fi
local name deleted=0
for name in "${PM2_APPS[@]}" "${PM2_APPS_LEGACY[@]}"; do
if pm2_app_exists "${name}"; then
log "pm2 delete ${name}"
pm2 delete "${name}" 2>/dev/null || true
deleted=$((deleted + 1))
fi
done
if [[ "${deleted}" -gt 0 ]]; then
pm2 save 2>/dev/null || true
fi
}
pm2_save_startup() { pm2_save_startup() {
step "PM2 save & startup" step "PM2 save & startup"
pm2 save 2>/dev/null || true pm2 save 2>/dev/null || true
+3 -8
View File
@@ -41,14 +41,9 @@ main_uninstall() {
echo "removed_dir=${removed_dir}" echo "removed_dir=${removed_dir}"
} >"${backup_dir}/uninstall.manifest" } >"${backup_dir}/uninstall.manifest"
step "停止并清空 PM2" step "停止并移除本项目 PM2 进程(不影响其它 PM2)"
if command -v pm2 >/dev/null 2>&1; then pm2_stop_project_apps
pm2 stop all 2>/dev/null || true pm2_delete_project_apps
pm2 delete all 2>/dev/null || true
pm2 save 2>/dev/null || true
else
log "未安装 pm2,跳过"
fi
remove_backup_cron_all remove_backup_cron_all
+9 -3
View File
@@ -26,6 +26,8 @@ fi
DEPLOY_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" DEPLOY_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_SOURCE="${DEPLOY_DIR}/reinstall.sh" SCRIPT_SOURCE="${DEPLOY_DIR}/reinstall.sh"
REPO_ROOT="$(cd "${DEPLOY_DIR}/.." && pwd)" REPO_ROOT="$(cd "${DEPLOY_DIR}/.." && pwd)"
# shellcheck source=lib/common.sh
source "${DEPLOY_DIR}/lib/common.sh"
INSTALL_ROOT="${INSTALL_ROOT:-/opt/crypto_monitor}" INSTALL_ROOT="${INSTALL_ROOT:-/opt/crypto_monitor}"
GIT_URL="${GIT_URL:-https://git.bz121.com/dekun/crypto_monitor.git}" GIT_URL="${GIT_URL:-https://git.bz121.com/dekun/crypto_monitor.git}"
@@ -230,10 +232,14 @@ backup_configs "${SRC_ROOT}" "${BACKUP_DIR}"
# --- 2. 停 PM2 --- # --- 2. 停 PM2 ---
step "停止并清空 PM2" step "停止并移除本项目 PM2 进程(不影响其它 PM2)"
if command -v pm2 >/dev/null 2>&1; then if command -v pm2 >/dev/null 2>&1; then
run pm2 stop all || true if [[ "${DRY_RUN}" -eq 1 ]]; then
run pm2 delete all || true log "[dry-run] pm2 stop/delete 仅: ${PM2_APPS[*]} (及历史别名)"
else
pm2_stop_project_apps
pm2_delete_project_apps
fi
else else
log "未安装 pm2,跳过" log "未安装 pm2,跳过"
fi fi