0

So I have this logrotate config file. I want to delete files that older than 2 days. After the first day, it add .1 to the filename of all the files that older than 2 days instead of deleting those files Then after the second day, still not deleting those file. I am not sure where I did wrong. But it works if I force run it logrotate -f '/etc/logrotate.d/configname'

Here's the config file I created

/data/adrouters/*/IAV/*/* /data/adrouters/*/logs/* /data/adrouters/*/SchOutIav/*/* /data/adrouters/*/SiteInfo/archive/* /data/logs/* {
    missingok
    rotate 1
    nocreate
    nodateext
    ifempty
    lastaction
        find /data/adrouters/ -type f -mtime +2 -exec rm \{} \;
        find /data/logs/ -type f -mtime +2 -exec rm \{} \;
    endscript
}
Paul
  • 93
  • 2
  • 15

2 Answers2

0

logrotate config file only specify the rules logrotate applies when running against the logfiles you specified.

You also need to add your logrotate execution line to your cronjob.

Adding cronjob as root(cronjob is based on different user!):

sudo su
crontab -e # <==== this will open an editor for you to edit your cronjobs, for root user

In the editor, you add an entry like this:

*/5 * * * * logrotate '/etc/logrotate.d/configname'

The "*/5 * * * " part is cron expression, it means executes every 5minutes, you need to change this to something make sense for you. In testing things out, you can set it to "/1 * * * *" and you can monitor on cron's log to see it actually happens.

Resource for cron: https://crontab.guru/

congbaoguier
  • 985
  • 4
  • 20
0

This is an issue with your wildcarding, not how you're running the cronjob.

Logrotate is not stateful; it does not keep track of which files are previously rotated logs instead of new log files.

The only way it can make this decision is by filename, upon execution. When you specify log paths with wildcards like these,

/data/adrouters/*/IAV/*/*
/data/adrouters/*/logs/* 
/data/adrouters/*/SchOutIav/*/* 
/data/adrouters/*/SiteInfo/archive/* 
/data/logs/*

logrotate is going to attempt to rotate every file in those directories as if they were new log files because * is going to match every file in that directory. That's what these lines are; you're telling logrotate which files to consider as new log files, to be rotated. If you expand to every file in that directory, it's never going to see an old file to rotate (or delete) accordingly.

You need to tighten up the wildcards you're using. Your log paths should never end in an asterisk unless you're rotating the old log files out of that directory using the olddir directive.

I've answered this before: Files are not removing in logrotate script and permissions and user,group details are not updated

parttimeturtle
  • 1,125
  • 7
  • 22