安装系统默认带有logrotate,不需要安装
    logrotate是用crontab定期运行的 ,默认配置文件是 /etc/cron.daily/logrotate
    配置具体要切割的文件配置,在/etc/logrotate.conf和/etc/logroate.d/*

    常用指令:
    1)测试配置文件执行效果:logroate -d xxx.conf
    2)检查各个文件切割状态:cat /var/lib/logrotate/logrotate.status

    常用配置:
    daily #每天执行
    dateext #后缀为日期
    missingok #没有可以不分割
    rotate 30 #30天循环
    compress #压缩
    delaycompress #当天不压缩
    notifempty #空不分割
    create 644 root root #分割后文件的权限
    sharedscripts # 指明脚本是共享的,多个log只会执行一次
    postrotate #分割后执行:
    #等连接的任务执行完,加载配置,再重启nginx
    [ -f /usr/local/nginx/nginx.pid ] && kill -USR1 cat /usr/local/nginx/nginx.pid
    endscript #postroate的结束符

    例子:

    1. $ cat /etc/logrotate.d/httpd
    2. /var/log/httpd/*log {
    3. missingok
    4. notifempty
    5. sharedscripts
    6. delaycompress
    7. postrotate
    8. /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    9. endscript
    10. }

    例子2:

    1. cat /etc/logrotate.d/nginx
    2. /var/log/nginx/*.log {
    3. daily
    4. dateext
    5. missingok
    6. rotate 30
    7. compress
    8. delaycompress
    9. notifempty
    10. create 644 root root
    11. sharedscripts
    12. postrotate
    13. [ -f /usr/local/nginx/nginx.pid ] && kill -USR1 `cat /usr/local/nginx/nginx.pid`
    14. endscript
    15. }

    例子3:
    docker容器的nginx可以用:kill -USR1 ps -ef | grep 'nginx: master process' | grep -v grep | awk '{print $2}'
    manual:

    1. LOGROTATE(8) System Administrator's Manual LOGROTATE(8)
    2. NAME
    3. logrotate ‐ rotates, compresses, and mails system logs
    4. SYNOPSIS
    5. logrotate [-dv] [-f|--force] [-s|--state file] config_file ..
    6. DESCRIPTION
    7. logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic
    8. rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when
    9. it grows too large.
    10. Normally, logrotate is run as a daily cron job. It will not modify a log more than once in one day unless the criterion
    11. for that log is based on the log's size and logrotate is being run more than once each day, or unless the -f or --force
    12. option is used.
    13. Any number of config files may be given on the command line. Later config files may override the options given in ear
    14. lier files, so the order in which the logrotate config files are listed is important. Normally, a single config file
    15. which includes any other config files which are needed should be used. See below for more information on how to use the
    16. include directive to accomplish this. If a directory is given on the command line, every file in that directory is used
    17. as a config file.
    18. If no command line arguments are given, logrotate will print version and copyright information, along with a short usage
    19. summary. If any errors occur while rotating logs, logrotate will exit with non-zero status.
    20. OPTIONS
    21. -?, --help
    22. Prints help message.
    23. -d, --debug
    24. Turns on debug mode and implies -v. In debug mode, no changes will be made to the logs or to the logrotate state
    25. file.
    26. -f, --force
    27. Tells logrotate to force the rotation, even if it doesn't think this is necessary. Sometimes this is useful
    28. after adding new entries to a logrotate config file, or if old log files have been removed by hand, as the new
    29. files will be created, and logging will continue correctly.
    30. -m, --mail <command>
    31. Tells logrotate which command to use when mailing logs. This command should accept two arguments: 1) the subject
    32. of the message, and 2) the recipient. The command must then read a message on standard input and mail it to the
    33. recipient. The default mail command is /bin/mail -s.
    34. -s, --state <statefile>
    35. Tells logrotate to use an alternate state file. This is useful if logrotate is being run as a different user for
    36. various sets of log files. The default state file is /var/lib/logrotate/logrotate.status.
    37. --usage
    38. Prints a short usage message.
    39. +-v, --verbose
    40. Turns on verbose mode, ie. display messages during rotation.
    41. CONFIGURATION FILE
    42. logrotate reads everything about the log files it should be handling from the series of configuration files specified on
    43. the command line. Each configuration file can set global options (local definitions override global ones, and later
    44. definitions override earlier ones) and specify logfiles to rotate. A simple configuration file looks like this:
    45. # sample logrotate configuration file
    46. compress
    47. /var/log/messages {
    48. rotate 5
    49. weekly
    50. postrotate
    51. /usr/bin/killall -HUP syslogd
    52. endscript
    53. }
    54. "/var/log/httpd/access.log" /var/log/httpd/error.log {
    55. rotate 5
    56. mail www@my.org
    57. size 100k
    58. sharedscripts
    59. postrotate
    60. /usr/bin/killall -HUP httpd
    61. endscript
    62. }
    63. /var/log/news/* {
    64. monthly
    65. rotate 2
    66. olddir /var/log/news/old
    67. missingok
    68. postrotate
    69. kill -HUP `cat /var/run/inn.pid`
    70. endscript
    71. nocompress
    72. }
    73. ~/log/*.log {}
    74. The first few lines set global options; in the example, logs are compressed after they are rotated. Note that comments
    75. may appear anywhere in the config file as long as the first non-whitespace character on the line is a #.
    76. The next section of the config file defines how to handle the log file /var/log/messages. The log will go through five
    77. weekly rotations before being removed. After the log file has been rotated (but before the old version of the log has
    78. been compressed), the command /sbin/killall -HUP syslogd will be executed.
    79. The next section defines the parameters for both /var/log/httpd/access.log and /var/log/httpd/error.log. Each is
    80. rotated whenever it grows over 100k in size, and the old logs files are mailed (uncompressed) to www@my.org after going
    81. through 5 rotations, rather than being removed. The sharedscripts means that the postrotate script will only be run once
    82. (after the old logs have been compressed), not once for each log which is rotated. Note that log file names may be
    83. enclosed in quotes (and that quotes are required if the name contains spaces). Normal shell quoting rules apply, with
    84. ', ", and \ characters supported.
    85. The next section defines the parameters for all of the files in /var/log/news. Each file is rotated on a monthly basis.
    86. This is considered a single rotation directive and if errors occur for more than one file, the log files are not com‐
    87. pressed.
    88. The last section uses tilde expansion to rotate log files in the home directory of the current user. This is only avail‐
    89. able, if your glob library supports tilde expansion. GNU glob does support this.
    90. Please use wildcards with caution. If you specify *, logrotate will rotate all files, including previously rotated
    91. ones. A way around this is to use the olddir directive or a more exact wildcard (such as *.log).
    92. Here is more information on the directives which may be included in a logrotate configuration file:
    93. compress
    94. Old versions of log files are compressed with gzip(1) by default. See also nocompress.
    95. compresscmd
    96. Specifies which command to use to compress log files. The default is gzip(1). See also compress.
    97. uncompresscmd
    98. Specifies which command to use to uncompress log files. The default is gunzip(1).
    99. compressext
    100. Specifies which extension to use on compressed logfiles, if compression is enabled. The default follows that of
    101. the configured compression command.
    102. compressoptions
    103. Command line options may be passed to the compression program, if one is in use. The default, for gzip(1), is
    104. "-6" (biased towards high compression at the expense of speed). If you use a different compression command, you
    105. may need to change the compressoptions to match.
    106. copy Make a copy of the log file, but don't change the original at all. This option can be used, for instance, to
    107. make a snapshot of the current log file, or when some other utility needs to truncate or parse the file. When
    108. this option is used, the create option will have no effect, as the old log file stays in place.
    109. copytruncate
    110. Truncate the original log file to zero size in place after creating a copy, instead of moving the old log file
    111. and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus
    112. might continue writing (appending) to the previous log file forever. Note that there is a very small time slice
    113. between copying the file and truncating it, so some logging data might be lost. When this option is used, the
    114. create option will have no effect, as the old log file stays in place.
    115. create mode owner group, create owner group
    116. Immediately after rotation (before the postrotate script is run) the log file is created (with the same name as
    117. the log file just rotated). mode specifies the mode for the log file in octal (the same as chmod(2)), owner
    118. specifies the user name who will own the log file, and group specifies the group the log file will belong to. Any
    119. of the log file attributes may be omitted, in which case those attributes for the new file will use the same val‐
    120. ues as the original log file for the omitted attributes. This option can be disabled using the nocreate option.
    121. createolddir mode owner group
    122. If the directory specified by olddir directive does not exist, it is created. mode specifies the mode for the
    123. olddir directory in octal (the same as chmod(2)), owner specifies the user name who will own the olddir direc‐
    124. tory, and group specifies the group the olddir directory will belong to. This option can be disabled using the
    125. nocreateolddir option.
    126. daily Log files are rotated every day.
    127. dateext
    128. Archive old versions of log files adding a date extension like YYYYMMDD instead of simply adding a number. The
    129. extension may be configured using the dateformat and dateyesterday options.
    130. dateformat format_string
    131. Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m %d %H and %s
    132. specifiers are allowed. The default value is -%Y%m%d except hourly, which uses -%Y%m%d%H as default value. Note
    133. that also the character separating log name from the extension is part of the dateformat string. The system clock
    134. must be set past Sep 9th 2001 for %s to work correctly. Note that the datestamps generated by this format must
    135. 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
    136. is not, since 01/11/2002 would sort lower while it is later). This is because when using the rotate option,
    137. logrotate sorts all rotated filenames to find out which logfiles are older and should be removed.
    138. dateyesterday
    139. Use yesterday's instead of today's date to create the dateext extension, so that the rotated log file has a date
    140. in its name that is the same as the timestamps within it.
    141. delaycompress
    142. Postpone compression of the previous log file to the next rotation cycle. This only has effect when used in com‐
    143. bination with compress. It can be used when some program cannot be told to close its logfile and thus might con‐
    144. tinue writing to the previous log file for some time.
    145. extension ext
    146. Log files with ext extension can keep it after the rotation. If compression is used, the compression exten‐
    147. sion (normally .gz) appears after ext. For example you have a logfile named mylog.foo and want to rotate it to
    148. mylog.1.foo.gz instead of mylog.foo.1.gz.
    149. hourly Log files are rotated every hour. Note that usually logrotate is configured to be run by cron daily. You have to
    150. change this configuration and run logrotate hourly to be able to really rotate logs hourly.
    151. ifempty
    152. Rotate the log file even if it is empty, overriding the notifempty option (ifempty is the default).
    153. include file_or_directory
    154. Reads the file given as an argument as if it was included inline where the include directive appears. If a direc‐
    155. tory is given, most of the files in that directory are read in alphabetic order before processing of the includ‐
    156. ing file continues. The only files which are ignored are files which are not regular files (such as directories
    157. and named pipes) and files whose names end with one of the taboo extensions, as specified by the tabooext direc‐
    158. tive.
    159. mail address
    160. When a log is rotated out of existence, it is mailed to address. If no mail should be generated by a particular
    161. log, the nomail directive may be used.
    162. mailfirst
    163. When using the mail command, mail the just-rotated file, instead of the about-to-expire file.
    164. maillast
    165. When using the mail command, mail the about-to-expire file, instead of the just-rotated file (this is the
    166. default).
    167. maxage count
    168. Remove rotated logs older than <count> days. The age is only checked if the logfile is to be rotated. The files
    169. are mailed to the configured address if maillast and mail are configured.
    170. maxsize size
    171. Log files are rotated when they grow bigger than size bytes even before the additionally specified time interval
    172. (daily, weekly, monthly, or yearly). The related size option is similar except that it is mutually exclusive
    173. with the time interval options, and it causes log files to be rotated without regard for the last rotation time.
    174. When maxsize is used, both the size and timestamp of a log file are considered.
    175. minsize size
    176. Log files are rotated when they grow bigger than size bytes, but not before the additionally specified time
    177. interval (daily, weekly, monthly, or yearly). The related size option is similar except that it is mutually
    178. exclusive with the time interval options, and it causes log files to be rotated without regard for the last rota‐
    179. tion time. When minsize is used, both the size and timestamp of a log file are considered.
    180. missingok
    181. If the log file is missing, go on to the next one without issuing an error message. See also nomissingok.
    182. monthly
    183. Log files are rotated the first time logrotate is run in a month (this is normally on the first day of the
    184. month).
    185. nocompress
    186. Old versions of log files are not compressed. See also compress.
    187. nocopy Do not copy the original log file and leave it in place. (this overrides the copy option).
    188. nocopytruncate
    189. Do not truncate the original log file in place after creating a copy (this overrides the copytruncate option).
    190. nocreate
    191. New log files are not created (this overrides the create option).
    192. nocreateolddir
    193. olddir directory is not created by logrotate when it does not exist.
    194. nodelaycompress
    195. Do not postpone compression of the previous log file to the next rotation cycle (this overrides the delaycompress
    196. option).
    197. nodateext
    198. Do not archive old versions of log files with date extension (this overrides the dateext option).
    199. nomail Do not mail old log files to any address.
    200. nomissingok
    201. If a log file does not exist, issue an error. This is the default.
    202. noolddir
    203. Logs are rotated in the directory they normally reside in (this overrides the olddir option).
    204. nosharedscripts
    205. Run prerotate and postrotate scripts for every log file which is rotated (this is the default, and overrides the
    206. sharedscripts option). The absolute path to the log file is passed as first argument to the script. If the
    207. scripts exit with error, the remaining actions will not be executed for the affected log only.
    208. noshred
    209. Do not use shred when deleting old log files. See also shred.
    210. notifempty
    211. Do not rotate the log if it is empty (this overrides the ifempty option).
    212. olddir directory
    213. Logs are moved into directory for rotation. The directory must be on the same physical device as the log file
    214. being rotated, unless copy, copytruncate or renamecopy option is used. The directory is assumed to be relative to
    215. the directory holding the log file unless an absolute path name is specified. When this option is used all old
    216. versions of the log end up in directory. This option may be overridden by the noolddir option.
    217. postrotate/endscript
    218. The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed (using
    219. /bin/sh) after the log file is rotated. These directives may only appear inside a log file definition. Normally,
    220. the absolute path to the log file is passed as first argument to the script. If sharedscripts is specified, whole
    221. pattern is passed to the script. See also prerotate. See sharedscripts and nosharedscripts for error handling.
    222. prerotate/endscript
    223. The lines between prerotate and endscript (both of which must appear on lines by themselves) are executed (using
    224. /bin/sh) before the log file is rotated and only if the log will actually be rotated. These directives may only
    225. appear inside a log file definition. Normally, the absolute path to the log file is passed as first argument to
    226. the script. If sharedscripts is specified, whole pattern is passed to the script. See also postrotate. See
    227. sharedscripts and nosharedscripts for error handling.
    228. firstaction/endscript
    229. The lines between firstaction and endscript (both of which must appear on lines by themselves) are executed
    230. (using /bin/sh) once before all log files that match the wildcarded pattern are rotated, before prerotate script
    231. is run and only if at least one log will actually be rotated. These directives may only appear inside a log file
    232. definition. Whole pattern is passed to the script as first argument. If the script exits with error, no further
    233. processing is done. See also lastaction.
    234. lastaction/endscript
    235. The lines between lastaction and endscript (both of which must appear on lines by themselves) are executed (using
    236. /bin/sh) once after all log files that match the wildcarded pattern are rotated, after postrotate script is run
    237. and only if at least one log is rotated. These directives may only appear inside a log file definition. Whole
    238. pattern is passed to the script as first argument. If the script exits with error, just an error message is shown
    239. (as this is the last action). See also firstaction.
    240. preremove/endscript
    241. The lines between preremove and endscript (both of which must appear on lines by themselves) are executed (using
    242. /bin/sh) once just before removal of a log file. The logrotate will pass the name of file which is soon to be
    243. removed. See also firstaction.
    244. rotate count
    245. Log files are rotated count times before being removed or mailed to the address specified in a mail directive. If
    246. count is 0, old versions are removed rather than rotated.
    247. size size
    248. Log files are rotated only if they grow bigger then size bytes. If size is followed by k, the size is assumed to
    249. be in kilobytes. If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size
    250. 100, size 100k, size 100M and size 100G are all valid.
    251. sharedscripts
    252. Normally, prerotate and postrotate scripts are run for each log which is rotated and the absolute path to the log
    253. file is passed as first argument to the script. That means a single script may be run multiple times for log file
    254. entries which match multiple files (such as the /var/log/news/* example). If sharedscripts is specified, the
    255. scripts are only run once, no matter how many logs match the wildcarded pattern, and whole pattern is passed to
    256. them. However, if none of the logs in the pattern require rotating, the scripts will not be run at all. If the
    257. scripts exit with error, the remaining actions will not be executed for any logs. This option overrides the
    258. nosharedscripts option and implies create option.
    259. shred Delete log files using shred -u instead of unlink(). This should ensure that logs are not readable after their
    260. scheduled deletion; this is off by default. See also noshred.
    261. shredcycles count
    262. Asks GNU shred(1) to overwrite log files count times before deletion. Without this option, shred's default will
    263. be used.
    264. start count
    265. This is the number to use as the base for rotation. For example, if you specify 0, the logs will be created with
    266. a .0 extension as they are rotated from the original log files. If you specify 9, log files will be created with
    267. a .9, skipping 0-8. Files will still be rotated the number of times specified with the rotate directive.
    268. su user group
    269. Rotate log files set under this user and group instead of using default user/group (usually root). user specifies
    270. the user name used for rotation and group specifies the group used for rotation. If the user/group you specify
    271. here does not have sufficient privilege to make files with the ownership you've specified in a create instruc‐
    272. tion, it will cause an error.
    273. tabooext [+] list
    274. The current taboo extension list is changed (see the include directive for information on the taboo extensions).
    275. If a + precedes the list of extensions, the current taboo extension list is augmented, otherwise it is replaced.
    276. At startup, the taboo extension list contains .rpmsave, .rpmorig, ~, .disabled, .dpkg-old, .dpkg-dist, .dpkg-new,
    277. .cfsaved, .ucf-old, .ucf-dist, .ucf-new, .rpmnew, .swp, .cfsaved, .rhn-cfg-tmp-*
    278. weekly [weekday]
    279. Log files are rotated once each weekday, or if the date is advanced by at least 7 days since the last rotation
    280. (while ignoring the exact time). The weekday intepretation is following: 0 means Sunday, 1 means Monday, ..., 6
    281. means Saturday; the special value 7 means each 7 days, irrespectively of weekday. Defaults to 0 if the weekday
    282. argument is omitted.
    283. yearly Log files are rotated if the current year is not the same as the last rotation.
    284. FILES
    285. /var/lib/logrotate/logrotate.status
    286. Default state file.
    287. /etc/logrotate.conf Configuration options.
    288. SEE ALSO
    289. gzip(1)
    290. <https://github.com/logrotate/logrotate>
    291. AUTHORS
    292. Erik Troan, Preston Brown, Jan Kaluza.
    293. <https://github.com/logrotate/logrotate>
    294. Linux Wed Nov 5 2002 LOGROTATE(8)