Logrotate: rotating log files based on max size

Mandeep Singh
1 min readAug 11, 2020

--

So recently, we were working on a task wherein we have to rotate our log files. The objective was to :
. Setup logrotate to rotate files based on size
. After rotation, make sure that process is able to write logs to the newly created log file

We selected two Linux utilities to do this work for us:
. Logrotate
.Cron

Both of the utilities were doing there work as expected. Logrotate was able to rotate the files as per config set and cron was triggering the logrotate command on a specified interval.
But there come the unknown part (which no one tells :( ), after the rotation the process generating log was not able to write logs to newly created log files. So we went through a couple of articles but didn’t get a working solution. One way suggested was to use copytruncate property of log rotate but we wanted to avoid this.
Finally, we were able to solve this problem. I am sharing the cron content and config below:
.cron
The cron file is created under :
directory: /etc/cron.d

Sample cron Content:

PATH=/usr/lib/sysstat:/usr/sbin:/usr/sbin:/usr/bin:/sbin:/bin#run command at every 15th minute
*/15 * * * * root bash -c ‘logrotate /etc/logrotate.d/<logroate_config_file_name>

Sample Logrotate config:

/var/log/<path_to_log_files> {
hourly
create 777 www-data www-data
rotate 24
maxsize 100M
compress
delaycompress
su root www-data
sharedscripts
postrotate
reload <service_name>
endscript
}

I hope this helps.

--

--