Install instructions:
- copy
rotate.timer
androtate.service
to/etc/systemd/system
- copy
rotate.sh
to/opt/bin
chmod +x /opt/bin/rotate.sh
systemctl daemon-reload
systemctl enable rotate.timer
systemctl start rotate.timer
[Unit] | |
Description=rotate log files | |
[Service] | |
Type=oneshot | |
ExecStart=/opt/bin/rotate.sh || true |
#!/bin/bash | |
rotate(){ | |
local available=$(df /var/log/journal | tail -n1 | awk '{print $4}'); | |
local max=$((5 * 1024)); | |
if [ $available -lt $max ];then | |
local logs=$(du -s /var/log/journal | awk '{print $1}'); | |
local possible=$(($logs + $available)); | |
if [ $possible -lt $max ]; then | |
echo "Only $possible space available. Did not rotate"; | |
return 1; | |
fi; | |
local free=$(($possible - $max)); | |
echo "Rotating to free $free" | |
journalctl --vacuum-size=$(($logs - $free)); | |
available=$(df /var/log/journal | tail -n1 | awk '{print $4}'); | |
if [ $available -lt $max ];then | |
echo "Could not clear enough logs"; | |
return 1; | |
fi; | |
else | |
echo "Nothing to do"; | |
fi; | |
return 0; | |
} | |
main(){ | |
rotate; | |
local rc=$?; | |
unset -f main; | |
unset -f rotate; | |
exit $rc; | |
} | |
main; |
[Unit] | |
Description=Rotate logs every 15 minutes | |
[Timer] | |
OnCalendar=*:0/15 | |
[Install] | |
WantedBy=timers.target |