#!/bin/bash set -euo pipefail SCRIPT_PATH="$(readlink -f "$0" 2>/dev/null || realpath "$0" 2>/dev/null || echo "$0")" SCRIPTNAME="$(basename "$SCRIPT_PATH")" LOCKFILE="/var/lock/${SCRIPTNAME}.pid" # ========= Configuration ========= CONTAINER_NAME="paperless-ai" OLLAMA_HOST="192.168.222.12" OLLAMA_PORT="11434" ERROR_PATTERN="Error: \[ERROR\] Document analysis failed: connect EHOSTUNREACH $OLLAMA_HOST:$OLLAMA_PORT" # Tools (adjust if your distro uses different names) WOL_CMD="wakeonlan" # or "etherwake -i eth0 $WOL_MAC" NC_CMD="nc" # netcat DOCKER="docker" log() { printf '[%(%F %T)T] %s\n' -1 "$*"; } # --- Single-instance using PID lockfile in /var/lock --- # Read old PID (if any) and terminate that instance if it is our script. if [[ -f "$LOCKFILE" ]]; then oldpid="$(cat "$LOCKFILE" 2>/dev/null || true)" if [[ "${oldpid:-}" =~ ^[0-9]+$ ]] && kill -0 "$oldpid" 2>/dev/null; then # Optional sanity check: ensure the old PID is this same script (best-effort). if grep -qa "$SCRIPTNAME" "/proc/$oldpid/cmdline" 2>/dev/null; then log "Found running instance (pid=$oldpid) → terminating" kill "$oldpid" 2>/dev/null || true # Give it a short grace period, then force if still alive. for _ in 1 2 3; do sleep 1 kill -0 "$oldpid" 2>/dev/null || break done kill -KILL "$oldpid" 2>/dev/null || true fi fi fi # Write our own PID to the lockfile (best-effort atomic via temp + move). tmp_lock="${LOCKFILE}.$$" printf '%d\n' "$$" > "$tmp_lock" mv -f "$tmp_lock" "$LOCKFILE" # Clean up lockfile on exit. cleanup() { [[ -f "$LOCKFILE" ]] && [[ "$(cat "$LOCKFILE" 2>/dev/null || true)" == "$$" ]] && rm -f "$LOCKFILE" || true } trap cleanup EXIT INT TERM # Follow container logs and look for the error pattern "$DOCKER" logs -f --since 0s "$CONTAINER_NAME" 2>&1 | \ while IFS= read -r line; do # echo "$line" # uncomment for debugging if echo "$line" | grep -qE "$ERROR_PATTERN"; then log "Detected EHOSTUNREACH for Ollama (${OLLAMA_HOST}:${OLLAMA_PORT})." fi done