Search code examples
linuxcentoslogrotate

Centos/Linux setting logrotate to maximum file size for all logs


we use logrotate and it runs daily ... now we have had some situations where logs have grown significantly (read: gigbaytes) and killing our server. So now we would like to set a maximum filesize to the logs ....

can I just add this to the logrotate.conf?

size 50M

and would it then apply to all log files? Or do I need to set this on a per log basis?

Or any other advice?

(ps. I understand that if you want to be notified is the log grows like described and what we want to do is not ideal - but it is better than not being able to logon anymore because there is no space available)

thanks, Sean


Solution

  • It specifies the size of the log file to trigger rotation. For example size 50M will trigger a log rotation once the file is 50MB or greater in size. You can use the suffix M for megabytes, k for kilobytes, and G for gigabytes. If no suffix is used, it will take it to mean bytes. You can check the example at the end. There are three directives available size, maxsize, and minsize. According to manpage:

    minsize size
                  Log  files  are  rotated when they grow bigger than size bytes,
                  but not before the additionally specified time interval (daily,
                  weekly,  monthly, or yearly).  The related size option is simi-
                  lar except that it is mutually exclusive with the time interval
                  options,  and  it causes log files to be rotated without regard
                  for the last rotation time.  When minsize  is  used,  both  the
                  size and timestamp of a log file are considered.
    
    size size
                  Log files are rotated only if they grow bigger then size bytes.
                  If size is followed by k, the size is assumed to  be  in  kilo-
                  bytes.  If the M is used, the size is in megabytes, and if G is
                  used, the size is in gigabytes. So size 100,  size  100k,  size
                  100M and size 100G are all valid.
    maxsize size
                  Log files are rotated when they grow bigger than size bytes even before
                  the additionally specified time interval (daily, weekly, monthly, 
                  or yearly).  The related size option is  similar  except  that  it 
                  is mutually exclusive with the time interval options, and it causes
                  log files to be rotated without regard for the last rotation time.  
                  When maxsize is used, both the size and timestamp of a log file are                  
                  considered.
    

    Here is an example:

    "/var/log/httpd/access.log" /var/log/httpd/error.log {
               rotate 5
               mail [email protected]
               size 100k
               sharedscripts
               postrotate
                   /usr/bin/killall -HUP httpd
               endscript
           }
    

    Here is an explanation for both files /var/log/httpd/access.log and /var/log/httpd/error.log. They are rotated whenever it grows over 100k in size, and the old logs files are mailed (uncompressed) to [email protected] after going through 5 rotations, rather than being removed. The sharedscripts means that the postrotate script will only be run once (after the old logs have been compressed), not once for each log which is rotated. Note that the double quotes around the first filename at the beginning of this section allows logrotate to rotate logs with spaces in the name. Normal shell quoting rules apply, with ,, and \ characters supported.