CentOS Linux uses logrotate to split management logs

  
 

The logrotate program is a log file management tool. Used to split log files, delete old log files, and create new log files to play the role of “dump”. Can save disk space.

logrotate command format: logrotate [OPTION...] <configfile>-d, --debug :debug mode, test the configuration file for errors. -f, --force : Forces the dump file. -m, --mail=command : Send logs to the specified mailbox. -s, --state=statefile : Use the specified state file. -v, --verbose : Shows the dump process. The logrotate configuration file is /etc/logrotate.conf. Check the default configuration:

cat /etc/logrotate.conf

Show as follows:

# see "man logrotate" for details# rotate log files weeklyweekly

# keep 4 weeks worth of backlogsrotate 4

# create new (empty) log files after rotating old onescreate

# uncomment this if you want your log files compressed#compress

# RPM packages drop log rotation information into this directoryinclude /etc/logrotate.d

# no packages own wtmp -- we'll rotate them here/var/log/wtmp {monthlyminsize 1Mcreate 0664 root Utmprotate 1}

# system-specific logs may be also be configured here.

Simple description: weekly: All log files are dumped once a week. Rotate 4 : The dumped file is divided into 4 copies. Create :logrotate automatically creates a new log file. Compress : Compresses the log file. The default is commented out. Include /etc/logrotate.d : Read the log dump parameters in the /etc/logrotate.d directory. When the RPM package is installed in the system, the log dump parameters of the RPM package are automatically created in /etc/logrotate. Under the .d directory. /var/log/wtmp section: Configuration of the /var/log/wtmp log dump.

Use logrotate to manage the connection log of nginx in the lnmp one-click installation package. The lnmp log file is in the /home/wwwlogs directory.

Create a configuration file:

vim /etc/logrotate.d/nginx

Enter the following:

/home/wwwlogs/access.log /home /wwwlogs/nginx_error.log {notifemptydailyrotate 5sharedscriptspostrotate/bin/kill -HUP `/bin/cat /usr/local/nginx/logs/nginx.pid`endscript}

Note: notifempty: if it is an empty file , do not dump. Daily : The log file is dumped once a day. Rotate 5 ; The dump file is divided into 5 copies. Postrotate/endscript : The script executed after the log dump. This is used to let nginx regenerate the log file. The nginx.pid contains the main process number of nginx.

Execute logrotate:

/usr/sbin/logrotate -vf /etc/logrotate.conf

If no error is reported, a dump file is generated and nginx is accessed normally. Ok.

How to auto-execute logrotate: There are scripts executed by logrotate in the /etc/cron.daily directory. Executed once a day through the crontab program.

Copyright © Windows knowledge All Rights Reserved