Linux log management service logrotate

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 includein

/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 crontab
  • rotate: How many historical files to keep
  • notifempty: If the file is empty, do not split the file, the default isifempty
  • nocompress: do not compress files
  • missingok: ignore file missing information
  • copytruncate: 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 loss
  • dateext: use date suffix
  • dateformat: the format of the date suffix
  • olddir: backup directory
  • postrotate/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.confYou can force the cutting to test whether it is effective

add to crontab

After the above test is no problem, crontab -eadd it to crontab

59 * * * * /path/to/logrotate /path/to/helloworld.conf >/dev/null 2>&1

Reference link

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/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324456737&siteId=291194637