2

I have an application which rotates its own log files on an hourly basis. Each hour I want to remove all of the files more then n days old. I have to use logrotate to do this because of business policy.

So what is the logrotate equivalent of running this command every hour?

find /var/log/app -name "*.old" -mtime +1 -exec rm -f {} \;
  • -type f also to be sure to delete file and nothing else – netmonk Sep 15 '15 at 12:51
  • 2
    did you have a look at man logrotate ? – Archemar Sep 15 '15 at 13:04
1

it's a possibility and you need to test :

cat /etc/logrotate.d/customapp

/var/log/app/*.old {
    daily
    missingok
    rotate 0
    notifempty
}

Why rotate 0 ?

If count is 0, old versions are removed rather than rotated. (source : man)

  • 2
    no, this wont work. logrotate will only remove files it created (rotated). source: testing, since docu doesnt cover it. – keen Jun 21 '16 at 20:33
0

logrotate runs once daily and the script can be found here, /etc/cron.daily/logrotate (on CentOS 7). If you want to run it each hour, you first need to move it to /etc/cron.hourly/. Then for the above command the equivalent logrotate script might look like this,

$ cat /etc/logrotate.d/app

/var/log/app/*.old {
  hourly
  rotate 0
  firstaction
    /usr/bin/find /var/log/app/ -name "*.old" -mtime +1 -delete
  endscript
  nocreate
  missingok
  notifempty
}
  • So basically you're saying that logrotate can't do what the OP wants, so, if they have a business policy that says they must use logrotate, then the solution is to use logrotate as a vehicle to run their find command every hour. OKAAAAAAY. I would warn the OP: this solution might be considered a policy violation. – Scott Nov 22 '18 at 5:44
  • If you don't put, firstaction /usr/bin/find /var/log/app/ -name "*.old" -mtime +1 -delete endscript logrotate will not remove the source log file. – Tanveer Nov 22 '18 at 6:36

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.