Browse Source

Update

main
Bastian (BaM) 3 months ago
parent
commit
2989a8190b
  1. 51
      auto-boot-ollama-pc.sh

51
auto-boot-ollama-pc.sh

@ -1,6 +1,9 @@
#!/bin/bash
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 =========
CONTAINER_NAME="paperless-ai"
@ -8,29 +11,49 @@ 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"
sleep 1
log() { printf '[%(%F %T)T] %s\n' -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
# --- 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
log() { printf '[%(%F %T)T] %s\n' -1 "$*"; }
# 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"
${DOCKER} logs -f --since 0s "${CONTAINER_NAME}" 2>&1 | \
while IFS= read -r line; do
# Print lines optionally for debugging:
#echo "$line"
# Clean up lockfile on exit.
cleanup() {
[[ -f "$LOCKFILE" ]] && [[ "$(cat "$LOCKFILE" 2>/dev/null || true)" == "$$" ]] && rm -f "$LOCKFILE" || true
}
trap cleanup EXIT INT TERM
if echo "$line" | grep -qE "${ERROR_PATTERN}"; then
# 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

Loading…
Cancel
Save