Use of logrotate in Linux Log File Manager, linuxlogrotate
Use of logrotate in Linux Log File Manager
Navigation
-Example 1: 17%
-Example 2 42%
-Example 3 46%
-Troubleshooting 52%
-1. Manually run logrotate53 %
-2. Rehearsal 57%
-3. Force round robin of 64%
-4. Logrotate record log 79%
-5. Logrotate: scheduled task 83%
Log files contain useful information about system events and are often used in troubleshooting or system performance analysis. For busy servers, the log file size will increase extremely fast, and the server will soon consume disk space, which becomes a problem. In addition, processing a single huge log file is often very tricky.
Logrotate is a very useful tool that automatically truncates (or rounds) logs, compresses and deletes old log files. For example, you can set logrotate to rotate/var/log/foo log files every 30 days and delete logs over 6 months. After configuration, logrotate is fully automated without further human intervention. In addition, old logs can also be sent by email, but this option is beyond the scope of this tutorial.
Logrotate packages are installed in mainstream Linux distributions by default. If logrotate does not appear for some reason, you can use apt-get or yum commands to install logrotate.
On Debian or Ubuntu:
# Apt-get install logrotate cron
# Yum install logrotate crontabs
# Touch/var/log-file
# Head-c 10 M </dev/urandom>/var/log-file
# Vim/etc/logrotate. d/log-file
/Var/log-file {
Monthly
Rotate 5
Compress
Delaycompress
Missingok
Notifempty
Create 644 root
Postrotate
/Usr/bin/killall-HUP rsyslogd
Endscript
}
Monthly: log files are collected on a monthly basis. Other available values are 'daily ', 'Weekly', or 'early '.
Rotate 5: Five archived logs are stored at a time. For the sixth archive, the most recent archive will be deleted.
Compress: after the round robin task is completed, the archive that has been Round Robin will be compressed using gzip.
Delaycompress: always used with the compress option. The delaycompress option instructs logrotate not to compress the latest archive. Compression will be performed in the next 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 errors such as "file not found.
Notifempty: If the log file is empty, the Round Robin will not proceed.
Create 644 root: create a New log file with the specified permissions. logrotate also renames the original log file.
Postrotate/endscript: after all other commands are completed, the specified commands in postrotate and endscript will be executed. In this case, the rsyslogd process immediately reads its configuration and continues running.
The template above is generic, and the configuration parameters are adjusted according to your needs. Not all parameters are necessary.
Example 2
In this example, we only want to cycle one log file, but the log file size can increase to 50 MB.
# Vim/etc/logrotate. d/log-file
/Var/log-file {
Size = 50 M
Rotate 5
Create 644 root
Postrotate
/Usr/bin/killall-HUP rsyslogd
Endscript
}
# Vim/etc/logrotate. d/log-file
/Var/log-file {
Monthly
Rotate 5
Dateext
Create 644 root
Postrotate
/Usr/bin/killall-HUP rsyslogd
Endscript
}
# Logrotate/etc/logrotate. conf
# Logrotate/etc/logrotate. d/log-file
# Logrotate-d/etc/logrotate. d/log-file
# Logrotate-vf/etc/logrotate. d/log-file
Reading config file/etc/logrotate. d/log-file
Reading config info for/var/log-file
Handling 1 logs
Rotating pattern:/var/log-file forced from command line (5 rotations)
Empty log files are rotated, old logs are removed
Considering log/var/log-file
Log needs rotating
Rotating log/var/log-file, log-> rotateCount is 5
Dateext suffix '-20140916'
Glob pattern '-[0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]'
Renaming/var/log/log-file.5.gz to/var/log/log-file.6.gz (rotatecount 5, logstart 1, I 5 ),
Old log/var/log/log-file.5.gz does not exist
Renaming/var/log/log-file.4.gz to/var/log/log-file.5.gz (rotatecount 5, logstart 1, I 4 ),
Old log/var/log/log-file.4.gz does not exist
...
Renaming/var/log/log-file.0.gz to/var/log/log-file.1.gz (rotatecount 5, logstart 1, I 0 ),
Old log/var/log/log-file.0.gz does not exist
Log/var/log/log-file.6.gz doesn't exist -- won't try to dispose of it
Renaming/var/log-file to/var/log/log-file.1
Creating new/var/log-file mode = 0644 uid = 0 gid = 0
Running postrotate script
Compressing log with:/bin/gzip
# Logrotate-vf-s/var/log/logrotate-status/etc/logrotate. d/log-file
# Cat/etc/cron. daily/logrotate
#! /Bin/sh
# Clean non existent log file entries from status file
Cd/var/lib/logrotate
Test-e status | touch status
Head-1 status> status. clean
Sed's/"// G' status | while read logfile date
Do
[-E "$ logfile"] & echo "\" $ logfile \ "$ date"
Done> status. clean
Mv status. clean status
Test-x/usr/sbin/logrotate | exit 0
/Usr/sbin/logrotate/etc/logrotate. conf