30

According the the Unix and Linux Administration Handbook and man, logrotate has options for daily, weekly, and monthly, but is there a way to add an hourly option?

This blog post mentions you can set size 1 and remove the time option (eg: daily) and then manually call logrotate with cron - I suppose something like

logrotate -f /etc/logrotate.d/my-hourly-file

but is there a more elegant solution for rotating logs hourly?

2
  • Sure. What is your operating system, what version?
    – Nils
    Jan 20, 2012 at 20:12
  • ubuntu 10.04 and 11
    – cwd
    Jan 20, 2012 at 21:22

4 Answers 4

28

Depending on your OS. Some (all?) Linux distributions have a directory /etc/cron.hourly where you can put cron jobs to be executed every hour.

Others have a directory /etc/cron.d/. There you can put cron-jobs that are to be executed as any special user with the usual cron-settings of a crontab entry (and you have to specify the username).

If you use either of these instead of the standard log rotatation script in /etc/cron.daily/ you should copy that script there and cp /dev/null to the original position. Else it will be reactivated by a logrotate patch-update.

For proper hourly rotation, also take care that the dateext directive is not set. If so, by default the first rotated file will get the extension of the current date like YYYYMMDD. Then, the second time logrotate would get active within the same day, it simply skips the rotation even if the size threshold has exceeded.

The reason is that the new name of the file to get rotated already exists, and logrotate does not append the content to the existing old file. For example on RHEL and CentOS, the dateext directive is given by default in /etc/logrotate.conf. After removing or commenting that line, the rotated files will simply get a running number as extension until reaching the rotate value. In this way, it's possible to perform multiple rotations a day.

1
  • Great, helped me a lot. To add for those who are looking for this, to disable dateext, add the tag "nodateext" in the config
    – Thiesen
    Sep 15, 2022 at 1:21
14

Just to add to Nils answer, if changing the location of the logrotate script on a Debian or Ubuntu box, it's probably safer to use dpkg-divert instead of just copying the file and copying /dev/null to the original position e.g.:

dpkg-divert --add --rename --divert /etc/cron.hourly/logrotate /etc/cron.daily/logrotate
2
  • I didn't know about this and I've been using Ubuntu for years. Thank you. :)
    – Vaughany
    Nov 17, 2017 at 14:59
  • Wouldn't ln -s /etc/cron.daily/logrotate /etc/cron.hourly/logrotate work? Aug 12, 2020 at 18:01
3

One other option would be adding the logrotate command into crontab list. Then it will execute for every hour.

crontab -e

add below line into crontab list

0 * * * * /usr/sbin/logrotate /etc/logrotate.d/my-hourly-file
2

Anyone coming back to this. Logrotate now has hourly option. You will have to put the logrotate script to the hourly cron though as it is in daily by default. Don't forget to remove it from the daily cron as it will run 2x in the same hour every day once at 3am~. (default cron.daily time afai

https://github.com/logrotate/logrotate

1
  • With hourly chosen, dateext now may be set too. By default, with hourly, dateext adds the two digit hour (from a 24-hour clock) at the end of the date suffix. From the logrotate man page on dateformat: "Specify the extension for dateext .... %Y %m %d %H %M %S %V and %s specifiers are allowed. The default value is -%Y%m%d except hourly, which uses -%Y%m%d%H ...." Mar 17 at 20:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .