TrumanWong

logrotate

System log rotation, compression and deletion

Supplementary instructions

logrotate command is used to rotate, compress, and delete system logs. It can also send logs to a specified mailbox. Using the logrotate command allows you to easily manage the log files generated by the system. Each log file can be set to be processed daily, weekly or monthly, or it can be processed immediately if the file is too large. You must edit it yourself and specify the configuration file. The default configuration file is stored in the /etc/logrotate.conf file.

grammar

logrotate(options)(parameters)

Options

-? or --help: online help;
-d or --debug: Display the instruction execution process in detail, which is convenient for troubleshooting or understanding the program execution situation;
-f or --force: Forcibly start the log file maintenance operation, even if the logrotate command deems it unnecessary;
-s<state file> or --state=<state file>: use the specified state file;
-v or --version: Display the instruction execution process;
-usage: Display the basic usage of the command.

Parameters

Configuration file: Specifies the configuration file for the lograote directive.

Example

crontab will call the logrotate command regularly and configure it in the /etc/cron.daily/logrotate file.

The logrotate configuration file /etc/logrotate.conf defines some custom log configurations that reference the /etc/logrotate.d directory.

Create a file with any suffix name in the /etc/logrotate.d directory to rotate the logs

/tmp/log/log.txt
{
     copytruncate
     daily
     rotate 30
     missingok
     ifempty
     compress
     noolddir
}

What this configuration file means is to rotate and compress the /tmp/log/log.txt file

compress compresses and dumps subsequent logs via gzip
nocompress does not perform gzip compression processing
copytruncate is used to back up and truncate the current log file for a log file that is still open; it copies first and then clears it. There is a time difference between copying and clearing, and some log data may be lost.
nocopytruncate backs up the log file but does not truncate it
create mode owner group specifies the attributes to create new files during rotation, such as create 0777 nobody nobody
nocreate does not create a new log file
When delaycompress and compress are used together, the dumped log file will not be compressed until the next dump.
nodelaycompress overrides the delaycompress option, dumping is compressed at the same time
missingok If the log is missing, continue scrolling to the next log without reporting an error.
errors address Error messages during storage are sent to the specified email address
ifempty Rotates the log file even if it is empty. This is the default option of logrotate.
notifempty When the log file is empty, no rotation is performed
mail address Send the dumped log file to the specified e-mail address
nomail does not send log files when dumping
olddir directory The dumped log file is placed in the specified directory, which must be in the same file system as the current log file.
noolddir The dumped log file and the current log file are placed in the same directory.
sharedscripts runs the postrotate script, which is executed once after all logs are rotated. If this is not configured, the script will be executed after each log rotation
prerotate Instructions that need to be executed before logrotate dumps, such as modifying file attributes and other actions; must be in separate lines
postrotate Instructions that need to be executed after the logrotate dump, such as restarting (kill -HUP) a service! Must be independent
daily specifies the dump period as daily
weekly specifies that the dump period is weekly
monthly specifies the dump period as monthly
rotate count specifies the number of dumps before deleting the log file, 0 means no backup, 5 means retaining 5 backups.
dateext uses the current date as the naming format
dateformat .%s is used with dateext. It appears immediately on the next line and defines the file name after file cutting. It must be used with dateext. Only the four parameters %Y %m %d %s are supported.
size(or minsize) log-size Dump only when the log file reaches the specified size

Precautions

Create a file with any suffix name in the /etc/logrotate.d directory

/tmp/log/log*
{
     copytruncate
     daily
     rotate 30
     missingok
     ifempty
     compress
     noolddir
}

In this case, the rotated log will be rotated again, because the file name after rotation also starts with log.