pez-infra/ansible/scripts/docker-log-cleanup.sh
Rasmus "Pez" Wejlgaard 9d56a22c30
Some checks are pending
Deploy (on merge) / Discover hosts (push) Waiting to run
Deploy (on merge) / deploy (push) Blocked by required conditions
Ansible-manage docker-log-cleanup script and cron (PESO-142) (#128)
docker-log-cleanup.sh lived in the repo but nothing deployed it — the
script and monthly cron on nuremberg-a were set up by hand and got wiped
when the host was reinstalled. Fold both into the docker role so every
docker_hosts member gets the script in /usr/local/bin and a monthly cron,
and it survives a rebuild.
2026-06-08 18:38:19 +01:00

20 lines
729 B
Bash
Executable file

#!/bin/bash
# Truncate large Docker container log files.
#
# Managed by Ansible (docker role) — deployed to /usr/local/bin/ on all
# docker_hosts and run monthly via cron. Do not edit on the host.
#
# Safety net for containers using the json-file log driver; most containers
# ship logs via the Loki driver and never write *-json.log, so on a healthy
# host this is usually a no-op.
LOG_DIR=/var/lib/docker/containers
MAX_SIZE_MB=100
find "$LOG_DIR" -name '*-json.log' | while read -r logfile; do
size_mb=$(du -m "$logfile" | cut -f1)
if [ "$size_mb" -gt "$MAX_SIZE_MB" ]; then
echo "$(date): Truncating $logfile (${size_mb}MB)" >> /var/log/docker-log-cleanup.log
truncate -s 0 "$logfile"
fi
done