Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

apache2 - Change time of Apache log rotation on Ubuntu

I am using Apache2 on Ubuntu 20.04. The access.log and error.log files are automatically rotated every day at midnight UTC. How do I change the time so that it, for example, rotates at 11:00 UTC?

This is the default config (/etc/logrotate.d/apache2), which I have not touched:

/var/log/apache2/*.log {
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                if invoke-rc.d apache2 status > /dev/null 2>&1; then 
                    invoke-rc.d apache2 reload > /dev/null 2>&1; 
                fi;
        endscript
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then 
                        run-parts /etc/logrotate.d/httpd-prerotate; 
                fi; 
        endscript
}

FYI, the reason I want to do this is that the rotation's associated graceful restart can sometimes cause an "Unable to connect to WSGI daemon process" issue, which is only solved by my doing a regular restart. Eventually, I'd like to solve that, but for now it's OK if I can just make sure that it happens at a more convenient time for me and my users.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can:

  • move out of the logrotate folder (/etc/logrotate.d) the apache2 rotation file
  • duplicate the /etc/logrotate.conf file and make sure the "include" options points to a folder containing the apache log rotation file
  • create a new entry in crontab to execute the new logrotate config file at the time you wish (crontab syntax: https://it.wikipedia.org/wiki/Crontab)

Another options would be:

  • [not required but suggested] change the path where apache stores logs
  • append date in the file name using a custom log format (something like 'CustomLog "|bin/rotatelogs -l /var/logs/logfile.%Y.%m.%d 86400" common')
  • [optionally] run a cronjob that compress the logs whose name doesn't match the pattern used in the CustomLog

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...