logrotate is a tool used by the operating system to manage logs. It supports log cutting, compression, cleaning, and email alarms. It runs regularly through the crontab service. You can also use this tool to manage our own service logs. Simply configure the configuration file. Just
logrotate configuration
helloworld.conf
, multiple logs can be written directly include
in
/path/to/log/hello.log
/path/to/log/world.err
{
hourly
rotate 24
notifempty
nocompress
missingok
copytruncate
dateext
dateformat .%Y%m%d%H
olddir /path/to/log/bk/
postrotate
echo "update to aws s3"
endscript
}
hourly
: Log cutting is done by hour, which is only supported after logrotate 3.9.0, but in fact the real scheduling is configured in crontabrotate
: How many historical files to keepnotifempty
: If the file is empty, do not split the file, the default isifempty
nocompress
: do not compress filesmissingok
: ignore file missing informationcopytruncate
: Create a copy of the log and truncate the old log, suitable for services that keep writing to a file without shutting down. As the process at this stage will take a short time, there may be data lossdateext
: use date suffixdateformat
: the format of the date suffixolddir
: backup directorypostrotate/endscript
: The command to be executed after cutting, this option is very flexible and can achieve some powerful functions, such as log uploading, log analysis, etc.
run
Direct execution logrotate -f helloworld.conf
You can force the cutting to test whether it is effective
add to crontab
After the above test is no problem, crontab -e
add it to crontab
59 * * * * /path/to/logrotate /path/to/helloworld.conf >/dev/null 2>&1
Reference link
- logrotate man: https://linux.die.net/man/8/logrotate
- logrotate date suffix supports hours: https://stackoverflow.com/questions/31132995/logrotate-dateformat-seems-not-supporting-hms
Please indicate the source when reprinting
this article: http://www.hatlonely.com/2018/04/16/linux-%E6%97%A5%E5%BF%97%E7%AE%A1%E7%90%86 %E6%9C%8D%E5%8A%A1-logrotate/