Recently, due to a recent project, but the project itself does not do log scrolling, resulting in log has been growing, so it will certainly burst the disk, resulting in unpredictable results this suitable logrotate appearedCopy the code
role
logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal,
and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.
Copy the code
As described by Man Logrotate, Logrotate can automatically rotate, compress, delete logs, and send emails
use
root@49335c6e5ee3:/var/log# logrotate --help Usage: logrotate [OPTION...] <configfile> -d, --debug Don't do anything, just test (implies -v) -f, --force Force file rotation -m, --mail=command Command to send mail (instead of `/usr/bin/mail') -s, --state=statefile Path of state file -v, --verbose Display messages during rotation -l, --log=STRING Log file or 'syslog' to log to syslog --version Display version information Help options: -? , --help Show this help message --usage Display brief usage messageCopy the code
The logrotate configfile file can be manually executed.
/var/log/cron.log {
daily
rotate 7
notifempty
create
size 1K
nocompress
nodateext
missingok
}
Copy the code
explain
parameter | instructions |
---|---|
daily | According to the days roll |
rotate | Number of files retained |
notifempty | If the log is empty, scroll |
create | New log files are created after the old log files are replaced |
size | Log scrolling is performed when the number of logs reaches |
nocompress | Log files are not compressed |
nodateext | Log files are incremented numerically, for example, log.1 |
missingok | If the log file does not exist, no error is reported |
For details, see Man Logrotate
Operation mechanism
For cron-installed systems,crontab will execute the script in /etc/cron.daily on a daily basis. This directory has a file called Logrotate:
#! /bin/sh test -x /usr/sbin/logrotate || exit 0 /usr/sbin/logrotate /etc/logrotate.confCopy the code
/usr/sbin/logrotate /etc/logrotate.conf /etc/logrotate.conf /usr/sbin/logrotate /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly
# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su root syslog
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# uncomment this if you want your log files compressed
#compress
# packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
missingok
monthly
create 0664 root utmp
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0660 root utmp
rotate 1
}
# system-specific logs may be configured here
Copy the code
Logrotate. d: include /etc/logrotate.d: include /etc/logrotate.d: include /etc/logrotate.d: include /etc/logrotate.d: include /etc/logrotate.d: include /etc/logrotate.d: include /etc/logrotate.d: include /etc/logrotate.d: include /etc/logrotate.d: include
/etc/logrotate.d/*
/etc/logrotate.conf
Copy the code
This article is published by OpenWrite, a blogging tool platform