Logrotate 

logrotate is a utility for managing log files in Unix-like operating systems. It is typically used to automate the rotation, compression, removal, and mailing of log files. The main purpose of log rotation is to prevent log files from consuming excessive disk space and to ensure that the logs are organized for easy management and analysis. 

  • Configuration File: logrotate is configured through a configuration file, usually located at /etc/logrotate.conf or in the /etc/logrotate.d/ directory. The main configuration file can include additional configuration files from the /etc/logrotate.d/ directory. 
  • Log Rotation Rules: In the configuration file, you define rules for log rotation. Each rule specifies a log file, the conditions under which rotation should occur (e.g., size, time), and the actions to be taken (e.g., compress, archive, delete). 
  • Rotation Triggers: Log rotation can be triggered based on various conditions, such as the size of the log file (size), the time elapsed since the last rotation (daily, weekly, monthly), or a combination of these conditions. 
  • Actions: After determining that a log file needs rotation, logrotate can perform various actions, including compressing the log file, renaming it with a timestamp, creating a new empty log file, and sending notifications. The actions are defined in the configuration file. 
  • Post-rotate and Pre-rotate Scripts: logrotate supports the execution of custom scripts before and after log rotation. This allows users to perform additional tasks, such as restarting a service to ensure it starts writing to the new log file. 
  • Compression: Log files can be compressed to save disk space. logrotate supports various compression methods, such as gzip, bzip2, and xz. 

Here’s a logrotate configuration file entry: 

  • Log rotation occurs weekly. 
  • Up to 4 rotated log files are kept. 
  • Rotation is triggered if the log file reaches 100 megabytes. 
  • The log files are compressed. 
  • A signal (SIGHUP) is sent to a process (example-daemon) after rotation. 

Recent Posts

Start typing and press Enter to search