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).

  • Do you want it to run at system startup, or service startup? Your title and your question body are at variance. – MadHatter Nov 7 '13 at 13:13
  • @MadHatter oops, i changed my mind while i wrote the question:-) for my system with the unstable clock it would be sufficient to rotate at system boot, but rotating at service startup seems like a good idea as well (so i get a new logfile for each run); i changed the title accordingly – umläute Nov 7 '13 at 13:18

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
        rotate 4

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.

  • i knew i forgot something about logrotate. copytruncate together with a forced rotation at startup is probably the way to go – umläute Nov 7 '13 at 13:20

Why bother with logrotate at all if you don't want/need to use it?

Simply rotate the log file from your start-up script.

  • because it nicely handles renaming of old files, which i don't see a point in re-implementing – umläute Nov 7 '13 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.