Logrotate

Logrotate Graphic
The obligatory AI generated creepy error-filled post image screen-clipped from search results.

I have no idea why this took me so long. I needed this for years, but every time I looked at it, I decided to put it off.

Well no more.

I finally got around to setting up log rotation on members of PiNet. Why? I'll explain.

When I write up some Python to do a thing, I always include the ability to write certain things to a log file so I can keep tabs on what's happening. Every time a new line gets added to the log file, it grows a little and takes a bit more storage space. Very much like all those magazines you have in that closet.

I have way too many computers (including Raspberry Pi's) around here doing things, and I sometimes forget to check the size of the log files. Sometimes they are rather large before I trim them down to size.

Logrotate is designed to handle this for me automatically. All I have to do is learn how to use it.

Logrotate is already running on every Linux device I have. So all I really had to do was read enough to grasp what configuration options I needed, then create the config files.

So it was a process of creating the config file, then running sudo logrotate -fv /etc/logrotate.d/newconfigname until I was happy with the results. You can also use sudo logrotate -d /etc/logrotate.d/yourconfigfilename instead to see what would happen when you run it for real. The -d parameter is for debug.

Because logrotate is already in use, it automatically picks up my new config files when it is launched by cron (the system scheduler).

The files live in the /etc/logrotate.d/ folder. They are pretty simple, actually.

Contents of file "/etc/logrotate.d/brilliant"

/home/greg/all/*.log {
        rotate 3
        daily
        olddir /var/log/brilliant
        nocompress
        missingok
        notifempty
        su root root
        create 644 greg greg
         }

This says: "Once every day, grab all the log files in the all folder. Move them to the /var/log/brilliant folder. Don't compress them. It's OK if one is missing that was there before. Don't bother if it's empty. Do this as the root user and member of the root group. Then go back and create new empty versions of those same log files as the user greg, and member of the greg group."

Brilliant required the su root root line, where weatherpi did not.

Contents of file "/etc/logrotate.d/weatherpi"

/home/greg/out/*.log {
        rotate 3
        daily
        olddir /var/log/weatherpi
        nocompress
        missingok
        notifempty
        create 644 greg greg
         }

Poolpi was happy with the same parameters as weatherpi. I'm guessing there's a difference in how Linux Mint (Ubuntu) handles users and how Raspian Buster does.

Bugpi also required the su root root line. It's running Raspian Bookworm which is newer than Buster.

Finally logrotate on the PiNet is a thing that works. Now why did this take me ten years?

link to home page

links

social

-->