You can use logrotate for this. CentOS 7 is using systemd so you can configure a scheduled logrotate task with something like this:
Create a logrotate config file in
mylogrotate.config with the following content:
This will tell logrotate to do the following:
- Old versions of log files are compressed with gzip by default.
- Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one.
- Postpone compression of the previous log file to the next rotation cycle so you're going to have two uncompressed log files.
- Log files are rotated when they grow bigger than 1Gbytes.
- If the log file is missing, go on to the next one without issuing an error message.
- Don't mail old log files to any address.
- Log files are rotated 30 times before being removed so you'll have 30 old log files. Anything older than that will be deleted.
Create a systemd service. Create a file in
mylogrotate.service with the following:
Description=Rotate My Log
ExecStart=/usr/sbin/logrotate /etc/systemd/system/mylogrotate.config --state /etc/systemd/system/mylogrotate.state --verbose
Let's schedule this task. Create a third file in
mylogrotate.timer with the following:
Description=Rotate My Log Timer
This will run logrotate every hour.
All has been set up. Now enable the scheduled task.
systemctl enable mylogrotate.timer
systemctl start mylogrotate.timer
Of course do all this as root.
(I'm on Ubuntu but I hope this will work on CentOS without any issues)