Logrotate – how to split log hourly on ubuntu or centos 6/7

Logrotate – how to split log hourly on ubuntu or centos 6/7

 

make hourly logrotate

Create an hourly working directory and copy the syslog you will use in the example.

sudo mkdir /etc/logrotate.hourly.d
sudo cp /etc/logrotate.d/syslog /etc/logrotate.hourly.d/syslog

Then type the following:

sudo vi etc/logrotate.hourly.d/syslog

/var/log/syslog
{ 
rotate 48
hourly
missingok
notifempty
delaycompress
compress
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}

Create sudo vi /etc/logrotate.hourly.conf and include the following to run the /etc/logrotate.hourly.d subtask with:

include /etc/logrotate.hourly.d

 

Now let cron create a logrotate file(sudo vi /etc/cron.hourly/logrotate) in the hourly directory and run the conf file you just created.

/usr/sbin/logrotate /etc/logrotate.hourly.conf

 

And with proper permissions to the conf and cron job files, the hourly job is ready to run.

sudo chmod 644 /etc/logrotate.hourly.conf
sudo chmod 755 /etc/cron.hourly/logrotate

 

 

Debug/Run immediately

You can check logrotate, but there is a simple way to check it. First, you can check whether it is running through debugging mode.

Debugging mode 

sudo logrotate -d /etc/logrotate.hourly.conf

 

If you need it right away, you can run it with -f.

Run mode

sudo logrotate -f /etc/logrotate.hourly.conf

Dateformat(legacy version)

Older versions that do not provide the Hourly option can be used with the dateformat option.

dateext
dateformat -%Y%m%d-%s

 

Add Comment

because parent directory has insecure permissions (It’s world writable or writable by group which is not “root”) Set “su” directive in config file to tell logrotate which user/group should be used for rotation

If you see the above error, you do not have sufficient privileges.

Enter the su root <owning group> in the conf file.

Facebook Comments

Leave A Reply

Detection ADBlockPlease, Disable or add to white list on our site.