Linux – make logrotate to only rotate at service startup

linuxlogrotate

I'd like to do logfile rotation for whenever i start a service, but never "automatically".

I'm running a python-based service that logs to a specific logfile.
the service (currently) does not support notification to re-open it's logfiles (e.g. USR1 trap).
Also, the service must not be restarted for logfile rotation (it's producing a continuous audio-stream, which should not be interrupted).

The system I'm running on does not have a hardware clock, so the clock gets reset every time the system boots, which makes log entries originating from different reboots in the same file tedious (i get the same date multiple times).

I also want to keep history across various boots.

So the idea is to use log-rotation on each boot (only).

Something like running /usr/sbin/logrotate --force /etc/logrotate.d/myservice before starting the service should do the trick.

Now my question is, is it possible to configure log-rotationg in /etc/logrotate.d/myservice in such a way, that it will be never run automatically?

An easy solution would be to not store my logrotate-config file in /etc/logrotate.d/, but "somewhere else".

But then, I'd like to have my user be able to find the logrotate-config in a well-known place (so they can modify the config at their will).

Best Answer

You can configure logrotate to rotate a file via copy-truncate, which preserves the original file and doesn't require you to re-create the log file.

The syntax is:

/tmp/output.log {
        size 1M
        copytruncate
        rotate 4
        compress
}

This will copy the current file, then cat /dev/null > file

That being said, logrotate only "automatically" rotates files in specific places, unless configured otherwise. I imagine you're logging your script to /var/log/httpd, and that's why it's being automatically rotated.