Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

logrotate.conf:

/abs/path/to/log/myapp-log.txt {
    daily
    copytruncate
    create 700 myUser myGroup
    dateext
    maxage 1
    missingok
}

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.

share|improve this question

1 Answer 1

up vote 6 down vote accepted
+100
  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
      
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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