Overview
logrotate periodically manages log files based on configuration files. Old logs are deleted or compressed according to retention periods to save disk space, and new log files are created to ensure stable system operation. It is automatically executed daily via cron jobs on most Linux systems.
Key Features
- Automatic log file rotation and archiving
- Compression and deletion of old log files
- Efficient disk space management
- Flexible rule application based on configuration files
- Included by default in most Linux distributions
Main Options
logrotate primarily operates through configuration files, but command-line options can be used to control specific actions.
Execution Control
File Specification
Generated command:
Try combining the commands.
Description:
`logrotate` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
logrotate is typically run automatically by cron, but it can be executed manually or tested with specific options.
Run logrotate with the default configuration file
sudo logrotate /etc/logrotate.conf
Although usually executed by cron, this command manually runs logrotate using the default configuration file.
Run in debug mode to preview changes
sudo logrotate -d /etc/logrotate.conf
Simulates log rotation without actually modifying log files, showing what logrotate would do. Essential for testing new configurations.
Force log file rotation
sudo logrotate -f /etc/logrotate.conf
Immediately rotates log files, ignoring rotation conditions (e.g., time, size). Use in emergencies or when specific logs need immediate processing.
Run with a specific application's configuration file
sudo logrotate -c /etc/logrotate.d/nginx
Executes logrotate using only the configuration file for a specific application located in the `/etc/logrotate.d/` directory.
Tips & Notes
Tips and important points for effectively using logrotate.
Configuration File Locations
Logrotate's configuration files are typically located in two main places:
- /etc/logrotate.conf: The global configuration file, which defines default settings and includes other configuration files applicable to all logs.
- /etc/logrotate.d/: A directory containing configuration files for individual applications (e.g., Nginx, Apache, MySQL) that define their specific log rotation rules.
Common Configuration Directives
Frequently used directives in logrotate configuration files:
- rotate N: Keep N rotated log files.
- daily/weekly/monthly/yearly: Specify the log rotation frequency.
- compress: Compress rotated log files.
- delaycompress: Compress the previous log file on the next rotation cycle.
- notifempty: Do not rotate the log file if it is empty.
- missingok: Do not generate an error if the log file is missing.
- create [mode owner group]: Create a new log file.
- postrotate/endscript: Define scripts to be executed after log rotation.
Testing Configuration Changes
Before applying new logrotate configurations, always test them using the `-d` (or `--debug`) option. This is the safest way to verify the validity of your settings without affecting the actual system.
Integration with cron
logrotate is typically executed automatically every day via the `/etc/cron.daily/logrotate` script. This script reads the `/etc/logrotate.conf` file to process all configured log rotation tasks.