2026-08-03
logpacker

automatic old log compresser script for freebsd

#!/bin/sh -Cefu


set -Cefu


cd script-directory || exit 1


if [ "${1:-}" = lockf ]
then
	shift
else
	lockf -s -t 0 "tmp/${0##*/}.lock" "./${0##*/}" lockf "$@"
	
	exit
fi


if [ ! -t 0 ]
then
	sleep "`jot -r 1 0 300`"
fi


find logs-directory -type f -not -name '*.xz' \
-not -name "*`date +%F`*" -mtime +3d -exec xz {} +

you can use this crontab (runs every day at 14:00)

0 14 * * * script-directory/compress-logs.sh

since script sleeps 0s ... 5min, actual run varies 14:00 ... 14:05

this is to avoid

https://en.wikipedia.org/wiki/Thundering_herd_problem

created [1785778398] 2026-08-03 20:33:18 +0300/EEST, modified [1785779665] 2026-08-03 20:54:25 +0300/EEST