linuxlogrotate

logrotate: daily rotation didn't work even though i used -f option


Here's the logrotate config file under /etc/logrotate.d/mylog

/var/www/www.mysite.com/logs/* {
        daily
        rotate 365
        compress
        delaycompress
        missingok
        notifempty
        create 0664 apache apache
}

I set this yesterday at 4 pm, and today at 10am i didn't see any compressed file.

I tried logrotate with -d and -f option

With -d option, i got this output

reading config file mysite
reading config info for /var/www/www.mysite.com/logs/*

Handling 1 logs

rotating pattern: /var/www/www.mysite.com/logs/*  after 1 days (365 rotations)
empty log files are not rotated, old logs are removed
considering log/var/www/www.mysite.com/logs/access-2018-08-08.log
  log does not need rotating

With -f option, nothing appeared.

But i noticed it added the .1 suffix to each of my log.

Anyone please explain what is this strange behavior please?


Solution

  • Logrotate does not have a built-in scheduling system.

    If you want to clean up logs manually once, sure you can run it manually:

    logrotate /etc/logrotate.d/mylog
    

    And if you check logrotate man page for -f behaviour:

    -f, --force
       Tells  logrotate  to force the rotation, even if it doesn't think this is necessary.  
       Sometimes this is useful after adding new entries to a logrotate config file, 
       or if old log files have been removed by hand, as the new files will be created, 
       and logging will continue correctly.
    
    -d, --debug
       Turns on debug mode and implies -v.  In debug mode, no changes will be made to 
       the logs or to the logrotate state file.
    

    So with -f option you forcefully rotate all logs thus the .1 suffix.

    And with -d option it is a dry run so you only preview the changes.

    Now if you want to run it daily, you have to make use of cron.

    By default after installation logrotate should have a default configuration file /etc/logrotate.conf and a default cron job /etc/cron.daily/logrotate

    You can either make use of them or create your own configuration file and cron job.

    Say if you place your custom configuration at /etc/logrotate.d/mylog then you can place the following cron job at /etc/cron.d/logrotate-cron to run it daily.

    0 0 * * * root /usr/sbin/logrotate /etc/logrotate.d/mylog --state /etc/logrotate.status
    

    You can check the file /etc/logrotate.status to make sure it ran.