Home > logrotate > Set new logrotate tasks to rotate apache logs in non-standard directories

Set new logrotate tasks to rotate apache logs in non-standard directories

I prefer compressing old logs to save space. Edit logrotate.conf

vi /etc/logrotate.conf

Uncomment the following line:

compress

Since I keep all my hosting’s logs in /home/hosting/log I need to add a new task to logrotate:

Create a new file under logrotate.d called hosting:

vi /etc/logrotate.d/hosting

Add the following contents to that file:

/home/hosting/*/log/*.log {
  weekly
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
        if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ]; then
            /etc/init.d/apache2 reload > /dev/null
        fi
    endscript
}

Explanation of script lines according to man page:

missingok
If the log file is missing, go on to the next one without issuing an error message.

rotate count
Log files are rotated times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather then rotated.
In our case we will remove logfiles older than 1 year (52 weeks)

compress
Duh!

delaycompress
Postpone compression of the previous log file to the next rotation cycle. This has only effect when used in combination with compress. It can be used when some program can not be told to close its logfile and thus might continue writing to the previous log file for some time.

notifempty
Do not rotate the log if it is empty (this overrides the ifempty option).

create mode owner group
Immediately after rotation (before the postrotate script is run) the log file is created (with the same name as the log file just rotated). mode specifies the mode for the log file in octal (the same as chmod(2)), owner specifies the user name who will own the log file, and group specifies the group the log file will belong to. Any of the log file attributes may be omitted, in which case those attributes for the new file will use the same values as the original log file for the omitted attributes. This option can be disabled using the nocreate option.

sharedscripts
Normally, prescript and postscript scripts are run for each log which is rotated, meaning that a single script may be run multiple times for log file entries which match multiple files (such as the /var/log/news/* example). If sharedscript is specified, the scripts are only run once, no matter how many logs match the wildcarded pattern. However, if none of the logs in the pattern require rotating, the scripts will not be run at all. This option overrides the nosharedscripts option and implies create option.
postrotate/endscript
The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated. These directives may only appear inside of a log file definition.

http://blog.melimato.com/wp-content/plugins/sociofluid/images/digg_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/reddit_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/stumbleupon_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/delicious_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/technorati_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/google_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/myspace_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/facebook_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/yahoobuzz_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/twitter_24.png http://blog.melimato.com/wp-content/plugins/sociofluid/images/meneame_24.png
Categories: logrotate Tags: , , , ,
  1. June 12th, 2009 at 19:47 | #1

    The best information i have found exactly here. Keep going Thank you

  2. June 14th, 2009 at 00:54 | #2

    The article is usefull for me. I’ll be coming back to your blog.

  3. June 19th, 2009 at 00:30 | #3

    Thanks for post. It’s very imformative read.
    I really like to browse blog.melimato.com!

    white rock teeth whitening

  4. July 2nd, 2009 at 20:23 | #4

    Hi Everybody :)

    I just wanted to conclude on the contribution of this community here. It’s great.

    I want to contribute my part to this forum as well

    There is a site that has been exceedingly helpful to myself and some associates of mine. That site is OnlineComputerHelpers.com and they offer online computer repair

    I hope that my aid has been substantial and you also are able to use their services just as I have.

  5. CrisBetewsky
    July 6th, 2009 at 19:59 | #5

    Some of us even don’t realize the importance of this information. What a pity.

  6. July 6th, 2009 at 22:15 | #6

    I have been looking looking around for this kind of information. Will you post some more in future? I’ll be grateful if you will.

  1. No trackbacks yet.

Comment moderation is enabled. Your comment may take some time to appear.

Additional comments powered by BackType