brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: maxsize makes logrotate rotate a log early when it grows too fast

Linux
~/tips/tip-logrotate-maxsize-early

maxsize 100M

Rotate a log on its normal schedule and also as soon as it passes a size.

Weekly rotation works fine until a service gets stuck in an error loop and its log is several gigabytes by Tuesday. logrotate can keep an eye on the size as well as the calendar, with maxsize.

/var/log/myapp/*.log {
    weekly
    maxsize 100M
    rotate 5
    compress
    missingok
}

The log still rotates once a week, but also as soon as it’s bigger than 100 MB, even before the week is over. Sizes take k, M and G. The opposite also exists: minsize rotates on the schedule, but only once the file has grown past the size you give, which suits a log that only gets a few lines a week.

Don’t confuse it with plain size. That one ignores the time interval completely, so a log with size 100M only ever rotates on size.

The caveat: logrotate isn’t a daemon that watches your files. It only checks when it runs, and the man page notes that it’s usually started once a day by cron or by logrotate.timer. A log that grows 20 GB in an afternoon still gets to 20 GB before the next run notices. If that’s a real risk for one service, run logrotate more often, and keep in mind that the man page says the same about hourly: the keyword alone doesn’t help until logrotate itself runs every hour.

Further reading: logrotate(8) man page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link