This article was reproduced from: http://linux008.blog.51cto.com/2837805/555829
Description:The Linux log file will fill the entire disk if it is not cleaned up regularly. This can be dangerous, so log management is one of the daily tasks of the system administrator. We can use "logrotate" to manage the Linux log files, it can achieve automatic scrolling log, log archiving and other functions. The following is an nginx log file to explain the use of logrotate.
Configuration:
1, in the/ETC/LOGROTATE.D directory to create a nginx configuration file "nginx" configuration content as follows
#vim/etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
Daily
Rotate 5
Missingok
Notifempty
Sharedscripts
Postrotate
if [-f/usr/local/nginx/logs/nginx.pid]; Then
KILL-USR1 ' Cat/usr/local/nginx/logs/nginx.pid '
Fi
Endscript
}
Save exit.
2, the implementation of Logrotate
#/usr/sbin/logrotate-f/etc/logrotate.d/nginx
The/usr/local/nginx/logs directory will produce
Error.log
Error.log.1
Description Logrotate configuration succeeded.
3. Let logrotate scroll once a day and add a line of timed scripts to the crontab.
#crontab-E
* * * */usr/sbin/logrotate-f/etc/logrotate.d/nginx
Log scrolling every 23:59
4. Configuration file Description
Daily: Log files are scrolled daily
Rotate: Retain the 5 scrolling log
Notifempty: Log file is empty and does not scroll
Sharedscripts: Run postrotate Script
Here is a script
Postrotate
if [-f/usr/local/nginx/logs/nginx.pid]; Then
KILL-USR1 ' Cat/usr/local/nginx/logs/nginx.pid '
Fi
Endscript
The script lets Nginx regenerate the log file.
============================ Simple Configuration ================================
More/etc/logrotate.d/nginx
/usr/local/nginx/logs/access.log {
Daily
Dateext
Rotate 10
Missingok
Notifempty
Copytruncate
}
Managing Nginx log files with Logrotate