How to use Logrotate Utility

Log management is a very essential work for a System Administrator. In this article I will show you how to manage your system/ application log files with logrotate utility. With logrotate utility we can accomplish below things:

  • Rotate the log files when the size reaches to a specified size.
  • Rotate the log files daily/ weekly/ monthly basis.
  • Compress the rotated log files.
  • Specify compression option for the rotated log files.
  • Rotate the log files with date.
  • Remove old rotated log files .

Basic Information:

  • Command Path: /usr/sbin/logrotate
  • Configuration file path: /etc/logrotate.conf
  • Configuration path for individual applications: /etc/logrotate.d

Usage:

NB: In this article I am using a log file named myapp.log locataed at /var/log

If you have a custom application and wants to add the log file to logrotate utility then you have to create a file inside the /etc/logrotate.d/ folder.

Rotate log files when reaches to a specific size:

As per the above configuration file the logrotate utility will do following things:

  • Rotate the log when the size is equal to or greater than 10 MB
  • Create a new log file with 700 permission. User and Group of the file will be root.
  • Keep 7 recent rotated log files.

Rotate log files with Compress:

The “compress” option will compress the files.

Rotate log file with date in the log file:

The “dateext” option will add date stamp to the rotated log files.

Rotate log files daily/ weekly/ monthly basis:

The option “daily” will rotate the logs files daily basis. Use “weekly” and “monthly” options as per your requirement.

Remove log files of a certain age:

The option “maxage” will remove the log files older than 30 days.

Additional Options and there usage:

  • copytruncate : Continue to write log information in the new file after log rotation.
  • postrotate – endscript : Will allow you to run a custom script after log rotation.
  • missingok : Will ignore the error if the the actual file is not available.
  • compresscmd /bin/bzip2 : Will allow you to add the compression command.
  • compressext .bz2 : Will allow you to add the extension to the rotated files.
The following two tabs change content below.

Tapas Mishra

Sr. Engineer (DevOps)
Loves to work on Opensource products. Having experience on Linux environment. Knowledge on Public cloud services like AWS, Rackspace, DigitalOcean, Linode. Please don't hesitate to give a comment on the posts. Your comments are my strength.

Leave a Reply