Update
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
SCRIPTNAME=$(basename "$(readlink -f "$0")")
|
|
||||||
|
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 =========
|
# ========= Configuration =========
|
||||||
CONTAINER_NAME="paperless-ai"
|
CONTAINER_NAME="paperless-ai"
|
||||||
@@ -8,29 +11,49 @@ OLLAMA_HOST="192.168.222.12"
|
|||||||
OLLAMA_PORT="11434"
|
OLLAMA_PORT="11434"
|
||||||
ERROR_PATTERN="Error: \[ERROR\] Document analysis failed: connect EHOSTUNREACH $OLLAMA_HOST:$OLLAMA_PORT"
|
ERROR_PATTERN="Error: \[ERROR\] Document analysis failed: connect EHOSTUNREACH $OLLAMA_HOST:$OLLAMA_PORT"
|
||||||
|
|
||||||
|
|
||||||
# Tools (adjust if your distro uses different names)
|
# Tools (adjust if your distro uses different names)
|
||||||
WOL_CMD="wakeonlan" # or "etherwake -i eth0 $WOL_MAC"
|
WOL_CMD="wakeonlan" # or "etherwake -i eth0 $WOL_MAC"
|
||||||
NC_CMD="nc" # netcat
|
NC_CMD="nc" # netcat
|
||||||
DOCKER="docker"
|
DOCKER="docker"
|
||||||
|
|
||||||
sleep 1
|
|
||||||
|
|
||||||
# Ensure only one instance of this script is running
|
|
||||||
if [ "$(pgrep -f $SCRIPTNAME)" != $$ ]; then
|
|
||||||
echo "Killing running instance of $SCRIPTNAME..."
|
|
||||||
pgrep -f -- "$SCRIPTNAME" | grep -vw "$$" | xargs -r kill -KILL
|
|
||||||
fi
|
|
||||||
|
|
||||||
log() { printf '[%(%F %T)T] %s\n' -1 "$*"; }
|
log() { printf '[%(%F %T)T] %s\n' -1 "$*"; }
|
||||||
|
|
||||||
${DOCKER} logs -f --since 0s "${CONTAINER_NAME}" 2>&1 | \
|
# --- Single-instance using PID lockfile in /var/lock ---
|
||||||
while IFS= read -r line; do
|
# Read old PID (if any) and terminate that instance if it is our script.
|
||||||
# Print lines optionally for debugging:
|
if [[ -f "$LOCKFILE" ]]; then
|
||||||
#echo "$line"
|
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
|
||||||
|
|
||||||
if echo "$line" | grep -qE "${ERROR_PATTERN}"; then
|
# 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})."
|
log "Detected EHOSTUNREACH for Ollama (${OLLAMA_HOST}:${OLLAMA_PORT})."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user