How to execute logrotate every hour

Logrotate does not support hourly schedule, but this is an easy task to accomplish.

Create separate directory to store hourly logrotate configuration files.

$ sudo mkdir /etc/logrotate.hourly.d

Create main logrotate configuration file that will read configuration files from designated directory.

$ cat << EOF | sudo tee /etc/logrotate.hourly.conf
# packages drop hourly log rotation information into this directory
include /etc/logrotate.hourly.d
EOF

Set proper persmissions.

$ sudo chmod 644 /etc/logrotate.hourly.conf

Create cron configuration to execute logrotate every hour and read main hourly configuration file.

$ cat << EOF | sudo tee /etc/cron.hourly/logrotate
#!/bin/bash
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.hourly.conf
EOF

Set proper persmissions.

$ sudo chmod 775 /etc/cron.hourly/logrotate

Create configuration file to hourly rotate specific log file inside above-mentioned directory.

$ cat << EOF | sudo tee /etc/logrotate.hourly.d/application
/var/log/application.log {
  size 128M
  rotate 1
  copytruncate
  missingok
  notifempty
  nocreate
  nomail
}

Set proper persmissions.

$ sudo chmod 644 /etc/logrotate.hourly.d/application

Do not specify weekly, monthly or yearly parameter.

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.