29

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?

Bernhard
  • 11,992
  • 4
  • 59
  • 69
cwd
  • 44,479
  • 71
  • 146
  • 167

4 Answers4

27

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.

airbjorn
  • 3
  • 3
Nils
  • 18,202
  • 11
  • 46
  • 82
  • 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 '22 at 01:21
13

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
heemayl
  • 54,820
  • 8
  • 124
  • 141
boltronics
  • 141
  • 1
  • 4
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
1

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