How to Rotate Apache Log Files in Linux

Embed Size (px)

Citation preview

  • 7/27/2019 How to Rotate Apache Log Files in Linux

    1/2

    How to Rotate Apache Log Files in Linux

    by Ramesh Natarajan on July 22, 2011

    Question: I would like to automatically rotate the apache access_log and error_log files. Canyou explain with an example on how to do this?

    Answer: This can be achived using logrotate utility as explained below.

    Add the following file to /etc/logrotate.d directory.

    # vi /etc/logrotate.d/apache/usr/local/apache2/logs/access_log /usr/local/apache2/logs/error_log {

    size 100Mcompressdateext

    maxage 30postrotate/usr/bin/killall -HUP httpdls -ltr /usr/local/apache2/logs | mail -s "$HOSTNAME: Apache restarted

    and log files rotated" [email protected]

    }

    Note: Refer to ourlogrotate tutorial(with 15 examples) that explains more details about how touse logrotate options.

    In the above /etc/logrotate.d/apache example:

    size 100MOnce the access_log, and error_log reaches 100M, it will be rotated. Youcan also use 100k (for Kb), 100G (for GB). Instead of size, you can also rotate apachelogs using frequency (daily, weekly, monthly).

    compressIndicates that the rotated log file will be compressed. By default this usesgzip. So, the rotated file will have .gz extension.

    dateext - Appends the date in YYYYMMDD format to the rotated log files. i.e Instead ofaccess_log.1.gz, it creates access_log-20110616.gz

    maxage - Indicates how long the rotated log files should be kept. In this example, it willbe kept for 30 days.

    postrotate and endscriptAny commands enclosed between these two parameter willbe executed after the log is rotated.

    Important: Once you rotate the log files, you want apache to write the new log messages to the

    newly created access_log and error_log. So, you need to send the HUP signal to the apache as

    shown here. Make sure to do /usr/bin/killall -HUP httpd, which will restart the apache afterrotating the log files (Read more aboutkill).

    http://www.thegeekstuff.com/2010/07/logrotate-examples/http://www.thegeekstuff.com/2010/07/logrotate-examples/http://www.thegeekstuff.com/2010/07/logrotate-examples/http://www.thegeekstuff.com/2009/12/4-ways-to-kill-a-process-kill-killall-pkill-xkill/http://www.thegeekstuff.com/2009/12/4-ways-to-kill-a-process-kill-killall-pkill-xkill/http://www.thegeekstuff.com/2009/12/4-ways-to-kill-a-process-kill-killall-pkill-xkill/http://www.thegeekstuff.com/2009/12/4-ways-to-kill-a-process-kill-killall-pkill-xkill/http://www.thegeekstuff.com/2010/07/logrotate-examples/
  • 7/27/2019 How to Rotate Apache Log Files in Linux

    2/2

    Also, you might want to send an email to yourself indicating that the log file is rotated, along

    with the output of ls -ltr command as the body of the email. i.e Add the following between

    postrotate and endscript option (after the killall command).

    ls -ltr /usr/local/apache2/logs | mail -s "$HOSTNAME: Apache restarted andlog files rotated" [email protected]

    The /etc/cron.daily/logrotate script runs everyday that will perform log rotate of all the files asspecified in the /etc/logrotate.conf and all the file under /etc/logrotate.d directory.

    After adding the above /etc/logrotate.d/apache file, for testing purpose, you can manually call thelogrotate script as shown below.

    # /etc/cron.daily/logrotate

    Once the log files are rotated, do a ls to verify them. As we explained above, the rotated log fileswill be kept for 30 days.

    # ls /usr/local/apache2/logsaccess_logerror_logaccess_log-20110716.gzerror_log-20110716.gz