Running logrotate for apache2 hourly not working

473 Views Asked by At

I'm trying to get logrotate to rotate my apache2 logs every hour (on the hour - at XX:xx:00), I know there are a few posts about this already, but it is just not working for me regardless of what I try, it continues to rotate daily. I'm running apache2 on Debian 11.

Here are the things I have already tried:

  • I have moved logtorate from cron.daily to cron.hourly
  • logtotate seems to point to /etc/logrotate.conf which contains the code below
  • I have changed the /etc/crontab to have the hourly cron run at 0 minutes (code below)
  • I have restarted apache2, logrotate and cron services.

Content of the logrotate file in cron.daily:

#!/bin/sh

# skip in favour of systemd timer
if [ -d /run/systemd/system ]; then
    exit 0
fi

# this cronjob persists removals (but not purges)
if [ ! -x /usr/sbin/logrotate ]; then
    exit 0
fi

/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit $EXITVALUE

Content of /etc/logrotate.conf

hourly
rotate 24
create
include /etc/logrotate.d

Inside /etc/logrotate there is an apache2 file which contains this:

/var/log/apache2/*.log {
    hourly
    missingok
    rotate 48
    notifempty
    create 640 root adm
    sharedscripts
    prerotate
    if [ -d /etc/logrotate.d/httpd-prerotate ]; then
        run-parts /etc/logrotate.d/httpd-prerotate
    fi
    endscript
    postrotate
    if pgrep -f ^/usr/sbin/apache2 > /dev/null; then
        invoke-rc.d apache2 reload 2>&1 | logger -t apache2.logrotate
    fi
    endscript
}

Content of /etc/crontab: SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 * * * *   root    cd / && run-parts --report /etc/cron.hourly
0 0 * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
0 0 * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
0 0 1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Any ideas on what other things I can try? I've run out of ideas!

UPDATE : Using John Hanleys suggestions I have been able to get close to what I'm hoping to ahcieve, but its not rotating at an exact time, despite indicating the following in the logrotate.timer the following:

hourly[Unit]
Description=Daily rotation of log files
Documentation=man:logrotate(8) man:logrotate.conf(5)

[Timer]
OnCalendar=hourly
AccuracySec=1m
Persistent=true

[Install]
WantedBy=timers.target

The result is that the logs are getting rotated every hour, but I need them rotating at a (more) exact time), this is what I currently have since making the changes yesterday:

-rw-r----- 1 root adm  18K May 23 18:54 access.log.14
-rw-r----- 1 root adm  11K May 23 19:59 access.log.13
-rw-r----- 1 root adm 1.3K May 23 20:54 access.log.12
-rw-r----- 1 root adm  927 May 23 21:25 access.log.11
-rw-r----- 1 root adm 3.5K May 23 22:52 access.log.10
-rw-r----- 1 root adm  534 May 23 23:46 access.log.9
-rw-r----- 1 root adm 1005 May 24 00:58 access.log.8
-rw-r----- 1 root adm  982 May 24 02:00 access.log.7
-rw-r----- 1 root adm 1.1K May 24 02:52 access.log.6
-rw-r----- 1 root adm 1.8K May 24 03:56 access.log.5
-rw-r----- 1 root adm 1.1K May 24 04:52 access.log.4
-rw-r----- 1 root adm  514 May 24 05:54 access.log.3
-rw-r----- 1 root adm 2.1K May 24 06:58 access.log.2
-rw-r----- 1 root adm 1.7K May 24 07:41 access.log.1

UPDATE: Well now I'm stumped! The error.log are rotating correctly, but the access.log are not:

-rw-r----- 1 root adm  1.6K May 24 08:59 access.log.10
-rw-r----- 1 root adm   740 May 24 09:00 error.log.10
-rw-r----- 1 root adm   549 May 24 09:25 access.log.9
-rw-r----- 1 root adm   740 May 24 10:00 error.log.9
-rw-r----- 1 root adm   464 May 24 10:38 access.log.8
-rw-r----- 1 root adm   370 May 24 11:00 error.log.8
-rw-r----- 1 root adm  3.9K May 24 11:51 access.log.7
-rw-r----- 1 root adm   658 May 24 12:00 error.log.7
-rw-r----- 1 root adm  5.0K May 24 12:03 access.log.6
-rw-r----- 1 root adm   370 May 24 13:00 error.log.6
-rw-r----- 1 root adm   589 May 24 13:57 access.log.5
-rw-r----- 1 root adm   370 May 24 14:00 error.log.5
-rw-r----- 1 root adm  1.3K May 24 14:54 access.log.4
-rw-r----- 1 root adm   370 May 24 15:00 error.log.4
-rw-r----- 1 root adm   452 May 24 15:58 access.log.3
-rw-r----- 1 root adm   370 May 24 16:00 error.log.3
-rw-r----- 1 root adm  6.4K May 24 16:56 access.log.2
-rw-r----- 1 root adm   370 May 24 17:00 error.log.2
-rw-r----- 1 root adm   889 May 24 17:54 access.log.1
-rw-r----- 1 root adm   691 May 24 18:00 error.log.1
0

There are 0 best solutions below