Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Fix QNAP agent duplicate processes during upgrades (#1317)

rcourtman01bf637d0d02
diff --git a/scripts/install.sh b/scripts/install.sh
index 2421a3c1bb..15369db3f4 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -58,11 +58,33 @@ LOG_FILE="/var/log/${AGENT_NAME}.log"
QNAP=false
QNAP_VOL=""
+qnap_kill_pidfile() {
+ local pidfile="$1"
+ local signal="${2:-TERM}"
+ local pid=""
+
+ [[ -f "$pidfile" ]] || return 0
+
+ pid="$(cat "$pidfile" 2>/dev/null || true)"
+ if [[ ! "$pid" =~ ^[0-9]+$ ]]; then
+ rm -f "$pidfile" 2>/dev/null || true
+ return 0
+ fi
+
+ [[ "$pid" == "$$" ]] && return 0
+ kill "-${signal}" "$pid" 2>/dev/null || true
+}
+
stop_qnap_agents_procfs() {
local signal="${1:-TERM}"
+ local qnap_state_dir="${INSTALL_DIR}"
+ local wrapper_script="${qnap_state_dir}/start-pulse-agent.sh"
+ local stored_binary="${qnap_state_dir}/${BINARY_NAME}"
+ local runtime_binary="/usr/local/bin/${BINARY_NAME}"
local proc=""
local pid=""
local cmd=""
+ local exe_path=""
[[ -d /proc ]] || return 0
@@ -72,30 +94,47 @@ stop_qnap_agents_procfs() {
[[ "$pid" == "$$" ]] && continue
cmd="$(tr '\000' ' ' < "$proc/cmdline" 2>/dev/null || true)"
+ exe_path="$(readlink "$proc/exe" 2>/dev/null || true)"
[[ -n "$cmd" ]] || continue
- case "$cmd" in
- *start-pulse-agent.sh*|*/pulse-agent*|*"/usr/local/bin/pulse-agent"*)
- kill "-${signal}" "$pid" 2>/dev/null || true
- ;;
- esac
+ if [[ "$cmd" == *"${wrapper_script}"* ]] || [[ "$cmd" == *"start-pulse-agent.sh"* ]]; then
+ kill "-${signal}" "$pid" 2>/dev/null || true
+ continue
+ fi
+
+ if [[ "$exe_path" == "$runtime_binary" ]] || [[ "$exe_path" == "$stored_binary" ]]; then
+ kill "-${signal}" "$pid" 2>/dev/null || true
+ # Match only when binary is the command (first arg), not a random argument
+ elif [[ "$cmd" == "${runtime_binary} "* ]] || [[ "$cmd" == "${runtime_binary}" ]] || \
+ [[ "$cmd" == "${stored_binary} "* ]] || [[ "$cmd" == "${stored_binary}" ]]; then
+ kill "-${signal}" "$pid" 2>/dev/null || true
+ fi
done
}
# Stop all QNAP pulse-agent processes (wrappers first, then binaries).
# Kill wrappers first to prevent watchdog respawn, then binaries.
stop_qnap_agents() {
+ local qnap_state_dir="${INSTALL_DIR}"
+ local watchdog_pidfile="${qnap_state_dir}/${AGENT_NAME}.watchdog.pid"
+ local agent_pidfile="${qnap_state_dir}/${AGENT_NAME}.pid"
+
# 1. Kill wrapper scripts (watchdog loops)
+ qnap_kill_pidfile "$watchdog_pidfile" TERM
pkill -f "start-pulse-agent\.sh" 2>/dev/null || true
stop_qnap_agents_procfs TERM
sleep 1
# 2. Kill agent binaries at both possible paths (with or without leading /)
+ qnap_kill_pidfile "$agent_pidfile" TERM
pkill -f "(^|/)pulse-agent( |$)" 2>/dev/null || true
stop_qnap_agents_procfs TERM
sleep 2
# 3. Verify — force-kill any survivors
+ qnap_kill_pidfile "$agent_pidfile" KILL
pkill -9 -f "(^|/)pulse-agent( |$)" 2>/dev/null || true
+ qnap_kill_pidfile "$watchdog_pidfile" KILL
stop_qnap_agents_procfs KILL
+ rm -f "$agent_pidfile" "$watchdog_pidfile" 2>/dev/null || true
}
# TrueNAS SCALE configuration (immutable root filesystem)
@@ -1304,6 +1343,9 @@ fi
if [[ "$QNAP" == true ]]; then
QNAP_STATE_DIR="${INSTALL_DIR}" # already set to ${QNAP_VOL}/.pulse-agent
STORED_BINARY="${QNAP_STATE_DIR}/${BINARY_NAME}"
+ WATCHDOG_PIDFILE="${QNAP_STATE_DIR}/${AGENT_NAME}.watchdog.pid"
+ AGENT_PIDFILE="${QNAP_STATE_DIR}/${AGENT_NAME}.pid"
+ LOCK_DIR="${QNAP_STATE_DIR}/${AGENT_NAME}.lock"
log_info "Binary installed to ${STORED_BINARY} (persistent storage)."
@@ -1325,11 +1367,73 @@ if [[ "$QNAP" == true ]]; then
# Pulse Agent startup script for QNAP
# Auto-generated by Pulse installer
+WATCHDOG_PIDFILE="${WATCHDOG_PIDFILE}"
+AGENT_PIDFILE="${AGENT_PIDFILE}"
+LOCK_DIR="${LOCK_DIR}"
+CURRENT_AGENT_PID=""
+
+kill_pidfile_process() {
+ pidfile="\$1"
+ signal="\${2:-TERM}"
+ pid=""
+
+ [ -f "\$pidfile" ] || return 0
+
+ pid=\$(cat "\$pidfile" 2>/dev/null || true)
+ case "\$pid" in
+ ''|*[!0-9]*)
+ rm -f "\$pidfile" 2>/dev/null || true
+ return 0
+ ;;
+ esac
+
+ [ "\$pid" = "\$\$" ] && return 0
+ kill "-\$signal" "\$pid" 2>/dev/null || true
+}
+
+cleanup_watchdog() {
+ agent_pid_to_cleanup="\$CURRENT_AGENT_PID"
+ owns_lock="false"
+
+ if [ -n "\$CURRENT_AGENT_PID" ]; then
+ kill "\$CURRENT_AGENT_PID" 2>/dev/null || true
+ wait "\$CURRENT_AGENT_PID" 2>/dev/null || true
+ CURRENT_AGENT_PID=""
+ fi
+
+ if [ -f "\$AGENT_PIDFILE" ]; then
+ pid=\$(cat "\$AGENT_PIDFILE" 2>/dev/null || true)
+ if [ "\$pid" = "\$\$" ] || [ -z "\$pid" ] || [ "\$pid" = "\$agent_pid_to_cleanup" ]; then
+ rm -f "\$AGENT_PIDFILE" 2>/dev/null || true
+ fi
+ fi
+
+ if [ -f "\$WATCHDOG_PIDFILE" ]; then
+ pid=\$(cat "\$WATCHDOG_PIDFILE" 2>/dev/null || true)
+ if [ "\$pid" = "\$\$" ] || [ -z "\$pid" ]; then
+ rm -f "\$WATCHDOG_PIDFILE" 2>/dev/null || true
+ owns_lock="true"
+ fi
+ fi
+
+ if [ "\$owns_lock" = "true" ]; then
+ rmdir "\$LOCK_DIR" 2>/dev/null || true
+ fi
+}
+
+handle_shutdown() {
+ cleanup_watchdog
+ exit 0
+}
+
stop_existing_agents() {
proc=""
pid=""
cmd=""
+ exe_path=""
+ kill_pidfile_process "\$WATCHDOG_PIDFILE" TERM
+ kill_pidfile_process "\$AGENT_PIDFILE" TERM
pkill -f "start-pulse-agent\.sh" 2>/dev/null || true
pkill -f "(^|/)pulse-agent( |\$)" 2>/dev/null || true
@@ -1340,20 +1444,66 @@ stop_existing_agents() {
[ "\$pid" = "\$\$" ] && continue
cmd=\$(tr '\000' ' ' < "\$proc/cmdline" 2>/dev/null || true)
+ exe_path=\$(readlink "\$proc/exe" 2>/dev/null || true)
[ -n "\$cmd" ] || continue
- case "\$cmd" in
- *start-pulse-agent.sh*|*/pulse-agent*|*"/usr/local/bin/pulse-agent"*)
- kill "\$pid" 2>/dev/null || true
- ;;
- esac
+ if [ "\${cmd#*"${WRAPPER_SCRIPT}"}" != "\$cmd" ] || [ "\${cmd#*start-pulse-agent.sh}" != "\$cmd" ]; then
+ kill "\$pid" 2>/dev/null || true
+ continue
+ fi
+
+ if [ "\$exe_path" = "${RUNTIME_BINARY}" ] || [ "\$exe_path" = "${STORED_BINARY}" ]; then
+ kill "\$pid" 2>/dev/null || true
+ # Match only when binary is the command (first arg)
+ elif [ "\${cmd%%\ *}" = "${RUNTIME_BINARY}" ] || [ "\${cmd%%\ *}" = "${STORED_BINARY}" ]; then
+ kill "\$pid" 2>/dev/null || true
+ fi
done
fi
+
+ sleep 1
+ kill_pidfile_process "\$AGENT_PIDFILE" KILL
+ kill_pidfile_process "\$WATCHDOG_PIDFILE" KILL
+ rm -f "\$AGENT_PIDFILE" "\$WATCHDOG_PIDFILE" 2>/dev/null || true
}
+acquire_watchdog_lock() {
+ lock_attempts=0
+ max_lock_attempts=5
+
+ while ! mkdir "\$LOCK_DIR" 2>/dev/null; do
+ lock_attempts=\$((lock_attempts + 1))
+ if [ "\$lock_attempts" -ge "\$max_lock_attempts" ]; then
+ echo "\$(date '+%Y-%m-%d %H:%M:%S') [watchdog] Stale lock detected, forcing removal" >> /var/log/${AGENT_NAME}.log
+ rm -f "\$AGENT_PIDFILE" "\$WATCHDOG_PIDFILE" 2>/dev/null || true
+ rmdir "\$LOCK_DIR" 2>/dev/null || true
+ if ! mkdir "\$LOCK_DIR" 2>/dev/null; then
+ echo "\$(date '+%Y-%m-%d %H:%M:%S') [watchdog] WARNING: Could not acquire lock, proceeding anyway" >> /var/log/${AGENT_NAME}.log
+ fi
+ break
+ fi
+
+ # Try to kill the existing lock holder
+ kill_pidfile_process "\$WATCHDOG_PIDFILE" TERM
+ kill_pidfile_process "\$AGENT_PIDFILE" TERM
+ sleep 1
+ kill_pidfile_process "\$AGENT_PIDFILE" KILL
+ kill_pidfile_process "\$WATCHDOG_PIDFILE" KILL
+ rm -f "\$AGENT_PIDFILE" "\$WATCHDOG_PIDFILE" 2>/dev/null || true
+ rmdir "\$LOCK_DIR" 2>/dev/null || true
+ sleep 1
+ done
+
+ echo "\$\$" > "\$WATCHDOG_PIDFILE"
+}
+
+trap 'handle_shutdown' INT TERM HUP
+trap 'cleanup_watchdog' EXIT
+
# Kill any running pulse-agent wrappers/binaries before starting this watchdog.
stop_existing_agents
sleep 2
+acquire_watchdog_lock
# Copy binary from persistent storage to /usr/local/bin (RAM disk, wiped on reboot).
# If the copy fails (e.g. no space), run directly from persistent storage instead.
@@ -1371,8 +1521,13 @@ MAX_RESTART_DELAY=60
while true; do
echo "\$(date '+%Y-%m-%d %H:%M:%S') [watchdog] Starting pulse-agent..." >> /var/log/${AGENT_NAME}.log
- \${AGENT_BIN} ${EXEC_ARGS} >> /var/log/${AGENT_NAME}.log 2>&1
+ \${AGENT_BIN} ${EXEC_ARGS} >> /var/log/${AGENT_NAME}.log 2>&1 &
+ CURRENT_AGENT_PID=\$!
+ echo "\$CURRENT_AGENT_PID" > "\$AGENT_PIDFILE"
+ wait "\$CURRENT_AGENT_PID"
EXIT_CODE=\$?
+ CURRENT_AGENT_PID=""
+ rm -f "\$AGENT_PIDFILE" 2>/dev/null || true
echo "\$(date '+%Y-%m-%d %H:%M:%S') [watchdog] pulse-agent exited with code \$EXIT_CODE, restarting in \${RESTART_DELAY}s..." >> /var/log/${AGENT_NAME}.log
sleep \$RESTART_DELAY