diff --git a/deploy_nvr.sh b/deploy_nvr.sh index e5119e2..c7acc21 100755 --- a/deploy_nvr.sh +++ b/deploy_nvr.sh @@ -39,18 +39,29 @@ if ! command -v sudo &>/dev/null; then fi fi -# ── 1. Kill all existing instances ── +# ── 1. Stop all existing instances ── log "Stopping any running NextNVR instances..." -sudo systemctl stop "${SERVICE_NAME}" 2>/dev/null || true -sudo pkill -9 -f "/opt/nextnvr/nextnvr" 2>/dev/null || true -sudo pkill -9 -f "go2rtc -config" 2>/dev/null || true -sudo pkill -9 -f "ffmpeg.*-i rtsp" 2>/dev/null || true + +# 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 + sleep 2 +fi + +# 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 -if pgrep -f "/opt/nextnvr/nextnvr" > /dev/null 2>&1; then - err "Cannot kill existing NextNVR processes. Please stop them manually and retry." - exit 1 -fi +# Final check — force kill stragglers. +for pid in $(pgrep -f "/opt/nextnvr/nextnvr" 2>/dev/null || true); do + sudo kill -9 "${pid}" 2>/dev/null || true +done + +sleep 1 log "All instances stopped." # ── 2. Detect greenfield vs update ── diff --git a/remove.sh b/remove.sh index 5724851..2d47fd8 100755 --- a/remove.sh +++ b/remove.sh @@ -11,17 +11,21 @@ echo "" # Stop service. echo "Stopping service..." -sudo systemctl stop "${SERVICE_NAME}" 2>/dev/null || true -sudo systemctl disable "${SERVICE_NAME}" 2>/dev/null || true +if systemctl is-active --quiet "${SERVICE_NAME}" 2>/dev/null; then + sudo systemctl stop "${SERVICE_NAME}" 2>/dev/null || true + sleep 2 +fi -# Kill any lingering processes (target only NextNVR binaries, not the script). -echo "Killing processes..." -sudo pkill -9 -f "/opt/nextnvr/nextnvr" 2>/dev/null || true -sudo pkill -9 -f "go2rtc -config" 2>/dev/null || true -sudo pkill -9 -f "ffmpeg.*-i rtsp" 2>/dev/null || true +# Kill remaining processes by PID (targeted). +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 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 sudo rm -f "/etc/systemd/system/${SERVICE_NAME}.service" sudo systemctl daemon-reload