How can I set up logrotate to rotate logs hourly?
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.
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
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