fix: graceful stop via systemctl + targeted kill, no pkill -9

This commit is contained in:
Claus Lohmar 2026-08-06 15:23:09 +01:00
parent 7b9902f788
commit a1f05012b4
2 changed files with 32 additions and 17 deletions

View file

@ -39,18 +39,29 @@ if ! command -v sudo &>/dev/null; then
fi fi
fi fi
# ── 1. Kill all existing instances ── # ── 1. Stop all existing instances ──
log "Stopping any running NextNVR instances..." log "Stopping any running NextNVR instances..."
# Graceful stop via systemd first.
if systemctl is-active --quiet "${SERVICE_NAME}" 2>/dev/null; then
sudo systemctl stop "${SERVICE_NAME}" 2>/dev/null || true sudo systemctl stop "${SERVICE_NAME}" 2>/dev/null || true
sudo pkill -9 -f "/opt/nextnvr/nextnvr" 2>/dev/null || true sleep 2
sudo pkill -9 -f "go2rtc -config" 2>/dev/null || true fi
sudo pkill -9 -f "ffmpeg.*-i rtsp" 2>/dev/null || true
# Kill any remaining processes by PID (targeted, no wildcard pkill).
for proc in "/opt/nextnvr/nextnvr" "go2rtc"; do
for pid in $(pgrep -f "${proc}" 2>/dev/null || true); do
sudo kill "${pid}" 2>/dev/null || true
done
done
sleep 2 sleep 2
if pgrep -f "/opt/nextnvr/nextnvr" > /dev/null 2>&1; then # Final check — force kill stragglers.
err "Cannot kill existing NextNVR processes. Please stop them manually and retry." for pid in $(pgrep -f "/opt/nextnvr/nextnvr" 2>/dev/null || true); do
exit 1 sudo kill -9 "${pid}" 2>/dev/null || true
fi done
sleep 1
log "All instances stopped." log "All instances stopped."
# ── 2. Detect greenfield vs update ── # ── 2. Detect greenfield vs update ──

View file

@ -11,17 +11,21 @@ echo ""
# Stop service. # Stop service.
echo "Stopping service..." echo "Stopping service..."
if systemctl is-active --quiet "${SERVICE_NAME}" 2>/dev/null; then
sudo systemctl stop "${SERVICE_NAME}" 2>/dev/null || true sudo systemctl stop "${SERVICE_NAME}" 2>/dev/null || true
sudo systemctl disable "${SERVICE_NAME}" 2>/dev/null || true sleep 2
fi
# Kill any lingering processes (target only NextNVR binaries, not the script). # Kill remaining processes by PID (targeted).
echo "Killing processes..." for proc in "/opt/nextnvr/nextnvr" "go2rtc"; do
sudo pkill -9 -f "/opt/nextnvr/nextnvr" 2>/dev/null || true for pid in $(pgrep -f "${proc}" 2>/dev/null || true); do
sudo pkill -9 -f "go2rtc -config" 2>/dev/null || true sudo kill "${pid}" 2>/dev/null || true
sudo pkill -9 -f "ffmpeg.*-i rtsp" 2>/dev/null || true done
done
sleep 1 sleep 1
# Remove systemd service file. # Remove systemd service.
sudo systemctl disable "${SERVICE_NAME}" 2>/dev/null || true
if [ -f "/etc/systemd/system/${SERVICE_NAME}.service" ]; then if [ -f "/etc/systemd/system/${SERVICE_NAME}.service" ]; then
sudo rm -f "/etc/systemd/system/${SERVICE_NAME}.service" sudo rm -f "/etc/systemd/system/${SERVICE_NAME}.service"
sudo systemctl daemon-reload sudo systemctl daemon-reload