logrotate man 中文版(自译)

原文来自: CentOS7 command man logrotate

NAME

logrotate ‐ rotates, compresses, and mails system logs

SYNOPSIS

logrotate [-dv] [-f|--force] [-s|--state file] config_file ..

DESCRIPTION

logrotate 设计用于避免系统管理产生的大量log文件。可以实现日志文件的自动切割,压缩,删除,邮件投递。每个日志文件可以按 daily、weekly、monthly的周期,或者增长到一定尺寸的时候处理。

通常,logrotate是以 daily 的计划任务执行。它并不会在一天内执行多次,除非日志处理的规则是按文件尺寸然后lograte一天内执行多次,或者使用了 -f or --force 参数。

可以在命令行指定多个config文件。后面的 config 文件会覆盖前面的,所以config文件排列的顺序很重要。通常,应该定义一个配置文件包含其他配置文件。下面会提供具体说明。如果命令行参数是一个目录,那么目录内的所有文件会被作为一个配置文件使用。

如果没有给出命令行参数,logrotate 会打印版本和版权信息,以及一个简单的使用介绍。如果在日志切割的时候报错,logrotate 会以 non-zero status退出。

OPTIONS

-?, --help
打印帮助

-d, --debug
开启 debug 模式,包含了 -v。 在 debug 模式下,不会对 logs 和 logrotate state file 做任何操作。

-f, --force
强制 logrotate 切割日志。通常在向logrotate config file 添加一些新内容后,强制执行切割。要不然就手动删除旧日志文件。当新文件创建后,日志再正常工作。

-m, --mail
配置 logrotate 发送邮件时使用的命令。该命令须要接受两个参数: 1) 邮件主题 subject , 2) 接受邮箱地址。该命令必须从标准输入读取信息,然后发送给接受邮箱。默认的mail命令是 /bin/mail -s.

-s, --state
配置 logrotate 使用替换状态文件 state file。在配置 logrotate 以不同用户运行,以适配不同的Log文件的时候会用到。 默认的状态文件是/var/lib/logrotate/logrotate.status。

--usage
打印简单使用帮助。

+-v, --verbose
打开 verbose 模式。

CONFIGURATION FILE

logrotate 从命令行指定的一系列配置文件读取相应的log文件。每个配置文件都可以设置全局属性 (本地定义会覆盖全局配置,后面定义的会覆盖前面定义的) 和指定日志切割。下面是一个简单的范例配置:

   # sample logrotate configuration file
   compress

   /var/log/messages {
       rotate 5
       weekly
       postrotate
           /usr/bin/killall -HUP syslogd
       endscript
   }

   "/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
   }

   /var/log/news/* {
       monthly
       rotate 2
       olddir /var/log/news/old
       missingok
       postrotate
           kill -HUP `cat /var/run/inn.pid`
       endscript
       nocompress
   }

   ~/log/*.log {}

前面几行设置全局属性;在范例中,日志在切割后会被压缩。备注以字符#作为开头,可以标记在配置文件的任何地方。

下个片段定义了如何处理日志文件 /var/log/messages。这个日志在被移除前,会以每周的频率切割5次(即保留5个切割文件)。在日子还文件被切割后 (在旧文件被压缩前),命令 /sbin/killall -HUP syslogd 会被执行。

再下个片段定义了如何同时处理 /var/log/httpd/access.log 和 /var/log/httpd/error.log。它们被配置超过100k的时候切割,在5次切割后,旧的日志文件(没有被压缩)会被邮件发送到 [email protected] ,而不是被移除。sharedscripts 意思是,postrotate script 将只被执行一次(在旧日志被压缩后), 而不是每个日志被切割的时候运行一次。将片段前的第一个文件名用双引号包含起来,允许 logrotate 以名字之间的空格来分割日志。 常见shell支持 '", and \ 字符作为包含规则。

再下个片段定义了如何处理 /var/log/news 目录下的所有文件。每个文件都以月周期切割。This is considered a single rotation directive and if errors occur for more than one file, the log files are not compressed.

最后一个片段使用波浪号扩展来切割当前用户目录下的所有日志文件。只有在你的glob library支持波浪号扩展的情况下才有用。 GNU glob 就不支持。

请小心使用通配符。如果你设置 *, logrotate 会切割所有的文件,包括先前已经切割生成的文件。可以使用 olddir 指令 或者 更准确的通配符来解决。

下面是有些在配置文件中可能会用到的指令的相关信息:

compress

老旧版本的log文件会被默认以gzip(1)压缩。相关: nocompress。

compresscmd

指定压缩命令。默认是 gzip。相关: compress。

uncompresscmd

指定解压命令。默认是 gunzip。

compressext

如果压缩被启用,指定压缩文件使用的扩展名。默认跟在配置的压缩命令后面。

compressoptions

被传到压缩程序的命令行参数。默认,传给 gzip(1) 的参数是 "-6" (以压缩速度的牺牲来实现高压缩率)。如果使用其他压缩命令,需要改变传递的参数。

copy

拷贝一份日志的副本,但是不改变源文件。这个选项可以用来创造一个当前日志文件的快照,或者当其他功能需要解析文件的时候。当开启这个选项后,create option 就会失效,原来的日志文件还工作(stay in place)。

copytruncate

在创建copy副本后裁剪源Log文件,而不是剪切原log文件创建一个新的。有些程序不能关闭日志文件,因此继续写在这个Log文件上。这个选项就可以用于这种情况。注意,在copy和裁剪之间会有一小时间片段,所以一些日志数据会丢失。 当开启这个选项后,create option 就会失效,原来的日志文件还工作。

create [mode] [owner] [group], create [owner] [group]

在切割后,log file立即(在postrotate脚本执行前)被创建(和切割的日志文件名一样)。 mode 以8进制指定log文件的mode属性 (同 chmod(2)), owner 指定log文件的用户,group 指定log文件属于哪个用户组。这些log文件的属性都可以被忽略,在这种情况下新创建文件的属性会保持于源文件相同。 可以使用nocreate参数,将这些参数禁用。

createolddir [mode] [owner] [group]

如果olddir指令指定的目录不存在,自动创建。mode 以8进制指定olddir目录的mode属性 (同 chmod(2)), owner 指定olddir目录的用户,group 指定olddir目录属于哪个用户组。这些log文件的属性都可以被忽略,在这种情况下新创建文件的属性会保持于源文件相同。 可以使用nocreateolddir参数,将这些参数禁用。

daily

每天执行

dateext

将老旧日志以时间扩展归档,YYYYMMDD 而不是简单的添加数字。扩展可以被配置使用 dateformat 和 dateyesterday 选项。

dateformat format_string

以类似于strftime(3) 函数的符号配置 dateext 的扩展格式。参数格式 %Y %m %d %H and %s 。默认值 %Y%m%d, hourly默认值%Y%m%d%H。 Note that also the character separating log name from the extension is part of the dateformat string. The system clock must be set past Sep 9th 2001 for %s to work correctly. Note that the datestamps generated by this format must be lexically sortable (i.e., first the year, then the month then the day. e.g., 2001/12/01 is ok, but 01/12/2001 is not, since 01/11/2002 would sort lower while it is later). This is because when using the rotate option, logrotate sorts all rotated filenames to find out which logfiles are older and should be removed.

dateyesterday

使用昨天的时间(而不是今天的时间)来创建 dateext extension, 这样切割的文件名字中会包含一个时间戳。

delaycompress

推迟先前的log文件压缩到下个切割周期进行。仅在使用compress的情况下有效。适用于,程序没有被告知关闭logfile因此还是在原logfile上写入的情况。

extension ext

Log 文件有扩展名的扩展符号(ext)可以在切割后保留。如果开启压缩,压缩的扩展名 (通常 .gz) 就跟在 ext 后面。 比如,有 logfile 名为 mylog.foo ,想切割为 mylog.1.foo.gz 而不是 mylog.foo.1.gz。

hourly

日志切割每小时执行一次。通常配置为每天执行一次。你需要在配置文件中指定每小时执行一次。

ifempty

即使日志为空也进行日志切割,覆盖 notifempty 参数 (ifempty 是默认参数)。

include [file_or_directory]

当使用include指令的时候,将给定的文件作为参数读取。如果给定的是目录,会在including files 进程继续前,将目录内的大部分文件按照字母的顺序读取作为参数。 有些文件忽略了,是因为那些文件不是 regular files (比如 directories 和 named pipes) 或者文件的名字以禁止的扩展名结尾, 正如 tabooext指令指定一样。

mail [address]

当一个log切割文件不该存在了,会被邮件发送到指定地址。如果有些日志不需要被mail发送,应该使用 nomail指定。

mailfirst

当使用 mail 命令的时候,只发送刚切割的文件 just-rotated file, 而不是快过期的文件 about-to-expire file。

maillast

当使用 mail 命令的时候,只发送 快过期的文件about-to-expire file,而不是刚生成的文件 just-rotated file (默认值).

maxage [count]

移除超过 天数的切割文件。 只有在logfile将被切割的时候才检查文件存在时长。文件会被mail发送到配置的地址,如果 maillast 和mail 被配置好的话。

maxsize [size]

当Log files增长超过maxsize值即切割,即使在设置的切割时间前 (daily, weekly, monthly, or yearly)。 相似的参数 size ,但是它不考虑时间区间选项,导致切割的时候不关心最后切割时间。当使用maxsize,那么文件的 size 和 timestamp 都会被考虑。

minsize [size]

当Log files增长超过minsize值即切克,但是不会优先于指定的时间 (daily, weekly, monthly, or yearly)前切割。 相似的参数 size ,但是它不考虑时间区间选项,导致切割的时候不关心最后切割时间。当使用minsize,那么文件的 size 和 timestamp 都会被考虑。

missingok

如果 log file 不存在,继续下一个,不报错。另,参见 nomissingok

monthly

按月切割文件。(通常是一个月的第一天).

nocompress

老旧的日志不压缩。 相关: compress。

nocopy

不拷贝源文件,源文件仍然工作。 (覆盖 copy 参数).

nocopytruncate

在创建一个copy副本后,不裁剪源文件 (覆盖 copytruncate 参数).

nocreate

不创建新日志文件 (覆盖 create 参数)。

nocreateolddir

旧文件的目录如果不存在,logrotate也不创建。

nodelaycompress

不推迟压缩动作到下个切割周期。 (覆盖 delaycompress 参数)。

nodateext

不将旧日志文件以时间归档。(覆盖 dateext 参数).

nomail

不使用mail发送旧文件到任何地址

nomissingok

如果日志文件不存在,抛出报错信息。这是默认值。

noolddir

在日志同样的目录进行日志切割。 (覆盖 olddir 参数)。

nosharedscripts

每个切割文件都运行 prerotate 和 postrotate 脚本 (默认值,且覆盖sharedscripts 参数)。Log文件的绝对路径作为第一个参数输入到脚本。 如果脚本报错退出,对于已经影响的log剩下的操作不会被执行。

vnoshred

不使用shred删除旧日志文件。另,参见 shred。

notifempty

如果文件是空,则不切割。(会覆盖 ifempty 参数)。

olddir directory

日志会被移动到指定目录进行切割。除非 copy, copytruncate or renamecopy参数被指定,该目录必须位于被切割日志同样的物理设备上。该目录路径为日志保存目录的相对路径,除非指定为绝对路径。当该参数被配置,所有旧日志会被移动到该目录。这个参数可以被 noolddir 参数覆盖。

postrotate/endscript

在日志被切割后,位于postrotateendscript 之间的行会被执行 (以 /bin/sh 执行) 。这些指令仅位于log文件定义部分。通常,日志文件的绝对路径会作为脚本的第一参数输入。如果sharedscripts被定义,所有的参数会被传递给脚本。另,参见prerotate。关于error handling参见sharedscriptsnosharedscripts

prerotate/endscript

在并且仅在日志被切割前,位于prerotateendscript 之间的行会被执行 (以 /bin/sh 执行) 。这些指令仅位于log文件定义部分。通常,日志文件的绝对路径会作为脚本的第一参数输入。如果sharedscripts被定义,所有的参数会被传递给脚本。另,参见postrotate。关于error handling参见sharedscriptsnosharedscripts

firstaction/endscript

在匹配的日志文件被切割之前,在prerotate脚本被运行前,且仅当至少一个文件被切割,位于firstactionendscript 之间的行被执行 (通过 /bin/sh) 。这些指令仅位于log文件定义部分。所有的参数会被作为第一个参数传递给脚本。如果脚本报错退出,将没有进一步行为。另,参见 lastaction

lastaction/endscript

在匹配的日志文件被切割后,在postrotate脚本被执行后,且仅当至少一个文件被切割,位于 lastactionendscript 之间的行会被执行 (通过 /bin/sh) 。这些指令仅位于log文件定义部分。所有的参数会被作为第一个参数传递给脚本。如果脚本报错退出,仅有一个报错信息提示(因为这是最后一个动作)。另,参见firstaction

preremove/endscript

在remove一个log文件前,位于preremoveendscript 之间的行会被执行 (通过 /bin/sh) 。 Logrotate 会pass 即将remove的文件的名字。另,参见 firstaction

rotate count

Log文件在被删除或者mail发送之前被切割的次数。如果设置为0,旧文件会被直接删除而不是切割。

size size

Log文件超过指定大小的时候被切割。如果 size 后缀是 k, 那么 size 单位是 kilobytes。如果后缀是 M ,那么 size 单位是 megabytes,如果后缀是 G , size 单位是 gigabytes。所以 size 100, size 100k, size 100M and size 100G 都是合法的。

sharedscripts

Normally, prerotate and postrotate scripts are run for each log which is rotated and the absolute path to the log file is passed as first argument to the script. That means a single script may be run multiple times for log file entries which match multiple files (such as the /var/log/news/* example). If sharedscripts is specified, the scripts are only run once, no matter how many logs match the wildcarded pattern, and whole pattern is passed to them. However, if none of the logs in the pattern require rotating, the scripts will not be run at all. If the scripts exit with error, the remaining actions will not be executed for any logs. This option overrides the nosharedscripts option and implies create option.

shred

使用shred -u而不是unlink()删除文件。 这需要确保logs在删除后不需要再被读取;默认是关闭的。另,参见 noshred。

shredcycles count

Asks GNU shred(1) to overwrite log files count times before deletion. Without this option, shred's default will be used.

start count

指定切割文件的起始计数。比如,如果设置为 0, 切割的logs文件会以 a .0 扩展方式命名。如果设置为 9, logs文件会以 a .9 开始命名,跳过 0-8。 文件会按照指令进行执行相应的切割次数。

su user group

不使用默认 user/group (通常是root),而是指定某个user 或 group 来切割Log 文件。 user 指定切割的用户名,group 指定切割的用户组。

tabooext [+] [list]

当前禁止扩展清单变更(查看 include 指令获取关于taboo extensions的信息)。如果扩展清单前有 + ,当前的禁止扩展清单增加,否则就是替代。 起先,禁止扩展清单包含如下扩展名 .rpmsave, .rpmorig, ~, .disabled, .dpkg-old, .dpkg-dist, .dpkg-new, .cfsaved, .ucf-old, .ucf-dist, .ucf-new, .rpmnew, .swp, .cfsaved, .rhn-cfg-tmp-* 。

weekly

当前weekday属性小于上次切割的weekday属性,或者自上次切割已经过去1周,进行日志切割。This is normally the same as rotating logs on the first day of the week, but it works better if logrotate is not run every night.

yearly

如果当前year属性于上次切割year属性不同,进行日志切割。

FILES

   /var/lib/logrotate/logrotate.status
                              Default state file.
   /etc/logrotate.conf        Configuration options.

SEE ALSO

   gzip(1)

   

AUTHORS

   Erik Troan, Preston Brown, Jan Kaluza.

   

Linux Wed Nov 5 2002 LOGROTATE(8)

你可能感兴趣的:(logrotate man 中文版(自译))