Logrotate

D

DevynCJohnson

Guest
The logs generated by Linux systems can take up a lot of disk space if left unmanaged. However, it may be inconvenient for admins to manually delete these logs every so often. Thankfully, most Linux systems use a special tool called "logrotate".

Logrotate is a program that compresses, deletes, and rotates logs. The cron tables initiate logrotate at specific intervals. Cron executes /etc/cron.daily/logrotate which is the script in the cron tables that initiates logrotate. Logrotate will then check the size of logs and manage them to reduce the size of the logs as a whole.

Logrotate can be run in a terminal with Root privileges if the user feels that it is necessary. The "-f" parameter will cause logrotate to rotate the logs even if logrotate would not normally do so.

Often times, users may want to adjust the settings of logrotate. The primary configuration file for logrotate is /etc/logrotate.conf. The logging for specific portions of the system (such as apt and cups) are configured with the configuration under /etc/logrotate.d/.

Running the shell command "cat /etc/logrotate.d/apt" will return the contents of the configuration file.

Code:
/var/log/apt/term.log {
  rotate 12
  monthly
  compress
  missingok
  notifempty
}

/var/log/apt/history.log {
  rotate 12
  monthly
  compress
  missingok
  notifempty
}
Each line/command does something specific to the logs. The log that is being managed comes before the curly brackets that enclose logrotate commands. Notice that there is one command per line. Many logrotate commands are available as seen below.

compress - If this option is listed, then gzip is used to compress the logs.

nocompress - The logs will not be compressed.

compresscmd - This command is used to indicate which command should be used to compress the logs. For instance, to use bzip2, then the line would be "compresscmd bzip2".

uncompresscmd - This command is used to indicate which command should be used to uncompress the logs. For instance, when using bzip2, then the line would be "compresscmd bunzip2".

compressext - Specify the file extension to use when compressing files. For instance, "compressext .old.gz" would be the extension for old logs that are compressed.

compressoptions - Specify the arguments to pass to the compression utility. For example, "compressoptions -9" would pass "-9" to gzip (if gzip is used).

copytruncate - The currently active logs (newest logs) will be copied as a backup. Then, the active logs will be truncated.

nocopytruncate - The currently logs are copied as a backup, but no manipulations occur to the active logs.

copy - Make a copy of the log, but do not change the original.

dateext - Add a date extension to the old logs.

dateformat - Specify the date format to use as a file extension. The syntax is similar to strftime. The only allowed specifiers are %Y, %m, %d, and %s. For instance, the command would look like "dateformat -%Y%m%d".

nodateext - Do not add date extensions to logs.

create - A new log file is created. This command uses the format "create mode owner group" to set the permissions, owner, and group settings.

nocreate - New logs are not created.

delaycompress - The rotated log (the created copy) is not immediately compressed. Rather, the log is compressed on the next cycle.

nodelaycompress - This is the opposite of "delaycompress".

errors - Mail error messages to the specified address (errors root).

ifempty - If the empty, it will still be rotated.

notifempty - The log is not rotated if it is empty.

mail - Mail the log to the specified address (mail root).

nomail - The mail logs are not sent to a user when they are rotated.

mailfirst - Mail the newly rotated file.

maillast - Mail the now expired log.

olddir - The old logs can be placed in a special place. The only parameter to this command is the path to the directory that will contain old logs.

noolddir - Rotated logs are kept in the same folder as the new/active logs.

prerotate & endscript - Commands that will be executed before the logs are rotated must be placed after prerotate and before endscript.

postrotate & endscript - Commands that will be executed after the logs are rotated must be placed after postrotate and before endscript.

maxage - Remove rotated logs older than X amount of days, so "maxage 7" is seven days.

minsize - The logs are rotated when they reach the specified size, but not before the specified interval. For example, the command "minsize 2000000" will rotate logs larger than 2kB after the specify interval (daily, weekly, etc.).

daily - This indicates that the logs are to be rotated daily.

weekly - This indicates that the logs are to be rotated weekly.

monthly - This indicates that the logs are to be rotated monthly.

rotate - This command indicates how many older sets of logs are kept. A parameter of 0 means no old logs are kept and 3 would indicate that 3 sets of older logs are kept.

missingok - If a log is missing, do not issue errors.

nomissingok - Issue errors if a log is missing.

shred - Deletes and shreds logs using "shred -u".

noshred - When deleting logs, do not use shred.

size - Specify the max size of a log. The accepted parameters include a number followed by a "k", "M", or "G" for kilobyte, megabyte, and gigabyte, respectively. The usage of this command looks like "size 500" for a 500 byte size limit. A kilobyte limit would look like "size 1k" or "size 1000".


For more info, view the man pages or visit http://linux.die.net/man/8/logrotate.
 

Attachments


Members online


Top