I'm trying to configure logrotate to rotate and delete log files every day at midnight, but have never used it before. So: (a) I want to confirm that my configuration is correct, and (b) I need help getting on automating its daily execution at midnight.

My software produces a large log file at /abs/path/to/log/myapp-log.txt. Here's what I'd like to happen:

  • Timestamp every log file with the date extension in its name
  • Only have 1 log file in /abs/path/to/log/ at any point in time; so every time the log is rotated and a new log file is created, delete the old one


/abs/path/to/log/myapp-log.txt {
    create 700 myUser myGroup
    maxage 1

A few things I'm unsure of here:

  • Do I need to specify daily if I'm specifying a maxage of 1?
  • What exactly is missingok doing? Something about ignoring a system warning if a log file isn't there? What happens if I don't specify missingok and the log file isn't there?
  • Do I even need copytruncate if I'm specifying daily/maxage 1? These three settings feel a little redundant, but not sure what role each one plays.

Second, how do I ensure this gets run every day at midnight? Cron? I only ask because I read somewhere that /etc/cron.daily/logrotate runs automatically every day, but not sure how that is configurable. Thanks in advance.

  1. Do I need to specify daily if I'm specifying a maxage of 1?

    Yes, you need daily as you want this section to execute everyday. This control how execution frequency of the section, while maxage control how long rotated files are kept. They are 2 different things.

  2. What exactly is missingok doing?

    missingok means logrotate will not complain/generate error if the log file for rotation does not exist. If this is not specified and the log file intended for rotation is missing, logrotate will generate an error log.

  3. Do I even need copytruncate if I'm specifying daily/maxage 1?

    If copytruncate is working for you now, don't change it. It apply to original log file

    copytruncate instruct logrotate to creates the copy of the original file (i.e rotate the original log file) and truncates the original file to zero byte size. This helps the respective service that belongs to that log file can write to the proper file.

  4. To run logrotate at mid-night

    There are 2 methods to do it.

    • Method 1 - Change when daily cron is run

      Look for following lines in /etc/crontab

      25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

      Change 25 6 (means 6:25AM), to 0 0. This also change ALL daily cron job starting time to mid-night.

    • Method 2 - Use custom crontab line

      Move logrotate out of default daily cron schedule

      mv /etc/cron.daily/logrotate /etc/logrotate.cronjob

      Create custom cron job. Add following line into /etc/crontab

      0 0 * * * root /etc/logrotate.cronjob
| improve this answer | |

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.