add logging to file

This commit is contained in:
trnq1ll0 committed 2025-10-25 20:24:17 +02:00
1 parent c64be0e9fa
commit 049f3aed84
1 file changed
+32 -1
+32 -1
View File
@@ -4,10 +4,29 @@
# 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:
@@ -17,15 +36,27 @@ cleanup() {
# 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
docker exec ix-photoprism-photoprism-1 photoprism index
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