logrotate Installation Configuration
Original link: https://linux.cn/article-4126-1.html
Logrotate is a useful tool that automatically truncates (or rounds) logs, compresses, and deletes old log files. installation
Logrotate installation is very simple. Yum or apt-get can be installed.
Yum-y Install Logrotate
Configuration file directory:/etc/logrotate.conf, which is usually not required to be modified. The rotation of the log file is set in a separate configuration file, and it is configured in the/etc/logrotate.d/directory
The following shows an example of three configuration files:
Example One
Create a log file for testing
Touch/var/log/test.log
head-c 20M </dev/urandom >/var/log/test.log
#填入一个20MB的随机比特流数据
Create the configuration file/etc/logrotate.d/test.conf and write:
/var/log/test.log{
monthly
rotate 5
compress
delaycompress
missingok
notifempty
Create 644 root root
postrotate
/usr/bin/killall-hup rsyslogd
endscript
}
#注释:
#monthly : The log file will be round by month. Other available values are ' daily ', ' weekly ' or ' yearly '.
#rotate 5:5 archive logs will be stored at a time. For a sixth archive, the oldest archive will be deleted.
#compress: After a round-robin task is completed, the archived archive will be compressed using gzip.
#delaycompress: Always with the Compress option, the Delaycompress option indicates that the logrotate does not compress the most recent archive, and the compression will be performed on the # next round-robin cycle. This is useful when you or any software still needs to read the latest archive.
#missingok: During log rotation, any errors will be ignored, such as "files cannot be found".
#notifempty: Round robin does not take place if the log file is empty.
#create 644 Root root: Creates a completely new log file with the specified permissions, and logrotate renames the original log file.
#postrotate/endscript: After all other instructions have been completed, the commands specified inside postrotate and Endscript will be executed. In this case, the #rsyslogd process will immediately read its configuration again and continue to run.
Example Two
We only want to round a log file, but the log file size can grow to 50MB.
#/etc/logrotate.d/test.conf write:
/var/log/log-file {
size=50m
rotate 5
Create 644 root root
Postrotate
/usr/bin/killall-hup rsyslogd
endscript
}
example Three
We want to have the old log file named after the creation date, which can be achieved by adding Dateext Changshu.
#/etc/logrotate.d/test.conf write:
/var/log/log-file {
monthly
rotate 5
dateext
Create 644 root Root
postrotate
/usr/bin/killall-hup rsyslogd
endscript
}
This will allow the archive to include date information in their file names. Test
Logrotate can be called manually from the command line at any time. To invoke Logrotate for all logs configured under/etc/lograte.d/:
[Root@node2 ~]# ls/etc/logrotate.d/
dracut redis salt syslog test.conf vsftpd Yum
[Root@node2 ~]# logrotate/etc/logrotate.conf
To call logrotate for a specific configuration:
[Root@node2 ~]# logrotate/etc/logrotate.d/test.conf
run in rehearsal mode
Use the '-d ' option to run logrotate in a preview fashion. To validate, you can simulate the walkthrough log rotation and display its output without actually tracking any log files.
[Root@node2 ~]# logrotate-d/etc/logrotate.d/test.conf
reading config file/etc/logrotate.d/test.conf
Reading Config info for/var/log/test.log
handling 1 logs
rotating pattern:/var/log/test.log Monthly (5 rotation s)
empty log files is rotated, old logs is removed
considering Log/var/log/test.log
log does not need R Otating not
running postrotate script, since no logs were rotated
As we can see from the above output, logrotate it is not necessary to judge the round-robin.
forced mode operation
[Root@node2 bin]# logrotate-vf/etc/logrotate.d/test.conf
reading config file/etc/logrotate.d/test.conf
Reading Config info for/var/log/test.log
handling 1 logs
rotating pattern:/var/log/test.log forced from command Line (5 rotations)
empty log files is not rotated, old logs is removed
considering Log/var/log/test.log
Lo G needs rotating
rotating log/var/log/test.log, Log->rotatecount is 5
dateext suffix ' -20150923 '
glob Pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] '
glob finding logs to compress failed Glob
finding old Rotated logs failed
renaming/var/log/test.log to/var/log/test.log-20150923
Creating New/var/log/test.log mode = 0644 UID = 0 gid = 0
running postrotate script
The crontab is then configured for timed processing.