How to manage log files in centos 7 using logrotate ?
What will be the best way to manage the log files in centos? How to configure the logrotate in centos 7?
How to use logrotate?
How to rotate logs in Linux ?
Firstly we need to check that logrotate is installed on the server or not. We will be managing the log files using the logrotate.
This method can used in centos 6 as well. All commands are the same and the process is also the same.
How to check the logrotate is installed ?
whereis logrotate
If logrotate is missing then install using the below command. Skip this step if allready installed.
yum update && yum install logrotate
Edit the httpd.conf file
vim /etc/httpd/conf/httpd.conf include /etc/logrotate.d
Edit the log rotate file for apache server
cd /etc/logrotate.d/httpd /var/log/httpd/*log { missingok notifempty sharedscripts delaycompress postrotate /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true endscript } /var/www/html/logs/*.log { daily rotate 3 size 10M compress delaycompress postrotate /bin/systemctl reload httpd reload > /dev/null 2>/dev/null || true endscript }
Check the log
what will happen if logrotate executed now. Try to use -d option (you can actually run logrotate by omitting this option):
logrotate -d /etc/logrotate.d/apache2.conf
Logrotate Important Terms.
Rotating the log means after a period of time old logs are zipped into the file. This helps us to maintain the logs very effectively.
missingok – do not output error if logfile is missing
notifempty – do not rotate log file if it is empty
compress – Old versions of log files are compressed with gzip(1) by default
size – Log file is rotated only if it grows bigger than 20k
daily – ensures daily rotation
create – creates a new log file wit permissions 600 where owner and group is root user
copy – Make a copy of the log file, but don’t change the original at all.
mail <email@address> – When a log is rotated out-of-existence, it is mailed to address.
olddir <directory> – Logs are moved into <directory> for rotation.
postrotate/endscript – The lines between postrotate and endscript are executed after the log