Skip to content

Instantly share code, notes, and snippets.

@Eeems
Last active February 19, 2020 16:27
Show Gist options
  • Save Eeems/a7505fdcabd69f508c0c8276ca351b66 to your computer and use it in GitHub Desktop.
Save Eeems/a7505fdcabd69f508c0c8276ca351b66 to your computer and use it in GitHub Desktop.
Force journalctl vacuum if less than 5M left on remarkable

Install instructions:

  • copy rotate.timer and rotate.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment