Simple Linux Log Rotation Example

CentOS Logrotate Example

While this article discusses log rotation in CentOS, you will want to also read my other article on tricking out the OS to allow instant rotation of files. You will find it here in Linux – Forcing Log Rotation – Get Past the Frustration.

Rotating Logs in Linux – Should be on by Default

Because there is a script file in /etc/cron.daily for logrotate already, it is scheduled and ready to go, we just need to specify which files we want to have it rotate for us.  While this example was done in CentOS, Linux Log Rotation is similar in most versions of Linux.

Additionally, the /etc/logrotate.conf file which configures the whole log rotation thing specifies this inclusion:

include /etc/logrotate.d

Add New Files for Logrotate to Rotate

All we have to do is add a file in the /etc/logrotate.d directory. In my case, I name it /etc/logrotate.d/uptimemadeeasy because it relates to my specific files for my applications.  While I used cat to create the file, you can use whatever editor you like when you create your file:

cat > /etc/logrotate.d/uptimemadeeasy <<EOF
/data/uptimemadeeasy/application1/logs/*.log {
daily                   # <– Run this daily
rotate 7              # <– Number of times a file is rotated before it is deleted.  Here, we keep a week’s worth.
dateext               # <– Add a date timestamp to the rotated files.
compress            # <– Compress the rotated files.
missingok           # <– If the file is missing, go to the next without error msgs.  Opposite is “nomissingok”
copytruncate       # <– Truncate the original file after creating a copy instead of moving the old file.
create 0664 deploy deploy   # <– Set the permissions and ownership after rotation
}
EOF

This will make it begin to consider the files in the specified directory for logrotation.

You can now manually run logrotation.  I like using the verbose option when running it manually so I can see what it is doing:

logrotate -v /etc/logrotate.conf

This will cause the logrotation to run manually.  I normally find that I have to wait a day for a new log rotation configuration to be run.

UME-logowtagline3002.jpg

 

The following two tabs change content below.
Jeff has 20 years of professional IT experience, having done nearly everything in his roles of IT consultant, Systems Integrator, Systems Engineer, CNOC Engineer, Systems Administrator, Network Systems Administrator, and IT Director. If there is one thing he knows for sure, it is that there is always a simple answer to every IT problem and that downtime begins with complexity. Seasoned IT professional by day, Jeff hopes to help other IT professionals by blogging about his experiences at night on his blog: http://uptimemadeeasy.com. You can find Jeff on or LinkedIn at: LinkedIn or Twitter at: Twitter

Latest posts by Jeff Staten (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *