#!/bin/bash # This script runs the index process of PhotoPrism. # it must be called from a cronjob or similar to ensure it runs periodically. # Flag-Datei zur Vermeidung paralleler Ausführungen FLAG_FILE="/root/photoprism-auto-index.flag" LOG_FILE="/root/photoprism-auto-index.log" MAX_LOG_SIZE=$((10 * 1024 * 1024)) # 10 MB in Bytes # Log-Funktion log() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" # Prüfe Log-Größe und kürze wenn nötig if [ -f "$LOG_FILE" ]; then LOG_SIZE=$(stat -f %z "$LOG_FILE" 2>/dev/null || echo 0) if [ "$LOG_SIZE" -gt "$MAX_LOG_SIZE" ]; then # Behalte nur die letzten 50% des Logs tail -n 5000 "$LOG_FILE" > "$LOG_FILE.tmp" mv "$LOG_FILE.tmp" "$LOG_FILE" echo "$(date '+%Y-%m-%d %H:%M:%S') - Log gekürzt (war $((LOG_SIZE / 1024 / 1024)) MB)" >> "$LOG_FILE" fi fi } # Cleanup-Funktion die IMMER ausgeführt wird (auch bei Fehlern/Abbruch) cleanup() { rm -f "$FLAG_FILE" log "Cleanup: Flag-Datei gelöscht" } # trap registriert die cleanup-Funktion für verschiedene Exit-Szenarien: # EXIT = normales Script-Ende # INT = CTRL+C (Signal 2) # TERM = kill-Befehl (Signal 15) # ERR = wenn ein Befehl fehlschlägt (bei set -e) trap cleanup EXIT INT TERM ERR log "Script gestartet (PID: $$)" # Prüfe ob ein anderer Prozess bereits läuft if [ -f "$FLAG_FILE" ]; then log "Abbruch: Prozess läuft bereits" trap - EXIT INT TERM ERR exit 1 fi log "Erstelle Flag-Datei" echo "Started: $(date '+%Y-%m-%d %H:%M:%S') | PID: $$" > "$FLAG_FILE" # Führe Index-Prozess aus log "Starte PhotoPrism Index-Prozess" docker exec ix-photoprism-photoprism-1 photoprism index >> "$LOG_FILE" 2>&1 EXIT_CODE=$? if [ $EXIT_CODE -eq 0 ]; then log "Index-Prozess erfolgreich abgeschlossen" else log "Index-Prozess fehlgeschlagen (Exit-Code: $EXIT_CODE)" fi # Flag-Datei wird automatisch durch trap cleanup gelöscht