Linux logs can grow uncontrollably, overwhelming disk space and slowing down your system. To prevent this, log rotation ensures old logs are archived or deleted when they reach a certain size or age, while maintaining critical log entries. The simplest solution is to use the built-in logrotate tool, which you can configure with a custom logrotate configuration file. This guide covers how to set up, manage, and troubleshoot log rotation properly.

What You'll Need

  • Root or sudo access to your Linux system to modify configuration files.
  • A basic understanding of command-line interfaces to manage log rotation settings.
  • Existing log files you want to rotate (most common are system logs like /var/log/syslog, /var/log/auth.log, or application-specific logs).
  • Optional: A text editor like nano, vim, or gedit for editing configuration files.

Step-by-Step How to Configure Log Rotation

  1. Check existing logrotate configuration files

    1. Navigate to the default logrotate configuration directory: sudo find /etc/logrotate.d/ to locate existing configurations, or sudo find /etc/logrotate.conf for the main configuration file.
    2. Review common logrotate entries, especially for system logs like syslog, auth.log, or application logs in /var/log/.
  2. Create or modify a custom logrotate configuration

    1. Create a new configuration file in /etc/logrotate.d/, naming it something descriptive like custom_app_logs.conf.
    2. Use a text editor to open the file, for example: sudo nano /etc/logrotate.d/custom_app_logs.conf and add the following basic template (replace parts in [] with your needs): /var/log/my_app.log { daily missingok rotate 7 compress delaycompress notifempty create 640 myapp myapp sharedscripts postrotate systemctl reload myapp.service endscript }

      Explanation of key parameters:

      • daily: Rotate logs daily (other options: weekly, monthly, or never).
      • rotate 7: Keep 7 copies of each log file.
      • compress: Compress rotated logs (using gzip or xz by default).
      • delaycompress: Wait until a log is empty to compress it.
      • missingok: Ignore missing log files without erroring.
      • notifempty: Skip rotation if the log file is empty.
      • create 640 myapp myapp: Create new log files with the specified permissions and ownership.
      • sharedscripts: Run postrotate commands for all files in this group.
      • postrotate: Execute commands after rotation (e.g., service reload).
  3. Test log rotation without triggering it immediately

    1. Manually simulate rotation using the logrotate command without arguments: sudo logrotate -d /etc/logrotate.d/custom_app_logs.conf This dumps the configuration to stdout for verification.
    2. If the configuration is correct, run the regular logrotate command to see if it generates errors: sudo logrotate -f /etc/logrotate.d/custom_app_logs.conf

      -f forces immediate rotation without waiting for the next scheduled run.

  4. Schedule log rotation with cron

    1. Check if logrotate is already scheduled: sudo crontab -l to review the current cron jobs.
    2. If you need to add a cron job, edit your user's crontab: sudo crontab -e Add this line to run logrotate daily at 3 AM: 30 3 * * * /usr/sbin/logrotate /etc/logrotate.conf

      Notes:

      • Use /etc/logrotate.conf for the main configuration file.
      • Cron paths in /usr/sbin/ may vary by system (check with which logrotate).
  5. Verify log rotation is working

    1. Check logrotate logs for errors: journalctl -u logrotate (on systems using systemd) or tail -f /var/log/syslog | grep logrotate to monitor its activity.
    2. Monitor your application logs directly to confirm rotation: tail -f /var/log/my_app.log and check for new rotated logs like my_app.log.7.gz.

Common Mistakes

  • Overcomplicating the configuration: Avoid excessive customization. Stick to core parameters like rotation frequency, file retention, and compression unless necessary. Complex settings can cause issues if you forget to update them.
  • Missing permissions: Ensure the logrotate configuration file and its directories are readable by root. A missing create directive or incorrect permissions can prevent log files from being created after rotation.
  • Ignoring postrotate commands: If you include sharedscripts or postrotate, ensure these commands execute correctly. Test them manually first to avoid downtime or configuration drift.
  • Not testing configurations: Always test logrotate with -d or -f before relying on it in production. A misconfigured logrotate can corrupt logs or skip critical entries.
  • Blocking file access during rotation: Avoid commands that hold file locks (e.g., tail or grep following rotation) during rotation to prevent log files from being overwritten.
  • Forgetting rotation in critical services: Log rotation should not disrupt services. If your postrotate command reloads a service, ensure it’s idempotent (i.e., it won’t fail if run multiple times).

Tips

  • Use logrotate -v for verbose output to debug issues:

    • sudo logrotate -v /etc/logrotate.d/custom_app_logs.conf
    • This shows each file processed, compressed, or skipped.
  • For system-wide logs, consider /etc/logrotate.d/ templates:

    • Use /etc/logrotate.d/syslog for system logs like syslog or auth.log.
    • Common templates include syslog, auth, and kern for kernel logs.
  • Backup critical logs before rotation:

    • Use a script to copy logs to a backup location before rotation: sudo cp /var/log/my_app.log /var/backups/my_app.log.bak before running logrotate.
  • Monitor disk space before enabling logrotate:

    • Check free space with df -h to ensure logs won’t fill up a small partition.
    • Consider using compressed logs for high-volume applications.
  • Use logrotate --debug for advanced troubleshooting:

    • This logs detailed information about file handling, permissions, and compression.
  • Document your logrotate setup:

    • Save a copy of your logrotate configuration in a version-controlled file or comment it in the central config.

Troubleshooting

  1. Logs aren’t rotating as expected

    1. Check if logrotate is running: sudo systemctl status logrotate or ps aux | grep logrotate to confirm it’s active.
    2. Verify the configuration file is being read: sudo cat /etc/logrotate.conf | grep -A 10 "custom_app_logs" to see if your file is included.
    3. Test with -f: sudo logrotate -f /etc/logrotate.d/custom_app_logs.conf to force rotation immediately.
    4. Check for permission issues: ls -la /etc/logrotate.d/custom_app_logs.conf and ensure root can read/write.
    5. If logs are stuck, try logrotate --force: sudo logrotate --force /etc/logrotate.conf
  2. Compression failures

    1. Ensure the compress directive uses a supported tool (e.g., gzip, xz).
    2. Check if xz or gzip is installed: which gzip or which xz to confirm.
    3. If compression fails, remove compress or delaycompress temporarily to test.
    4. Manually test compression: gzip -t my_app.log.7 to verify the file can be compressed.
  3. Postrotate commands failing

    1. Test postrotate commands manually: sudo /etc/logrotate.d/custom_app_logs.conf | grep postrotate then run the commands directly: sudo systemctl reload myapp.service to verify.
    2. If the service isn’t loaded, run: sudo systemctl enable myapp.service before testing.
    3. Check service status: sudo systemctl status myapp.service if the reload fails.
  4. Logs appear corrupted after rotation

    1. Verify no file locks are holding up rotation.
    2. Check for manual log appends during rotation: tail -f /var/log/my_app.log and stop new writes temporarily.
    3. If the issue persists, rotate logs manually first, then verify the rotated file is intact: tail -n 50 my_app.log.7 to check contents.
  5. Cron job not running logrotate

    1. Check cron logs: sudo grep CRON /var/log/syslog or journalctl -u cron to see if the job executed.
    2. Test the cron path manually: sudo /usr/sbin/logrotate /etc/logrotate.conf to confirm it works.
    3. If the path is incorrect, update the cron line: 30 3 * * * /usr/bin/logrotate /etc/logrotate.conf (replace /usr/sbin/ with the actual path).
  6. Logs are still filling disk space

    1. Increase rotation frequency or reduce retention: rotate 14 (keep 14 days of logs).
    2. Use notifempty to skip empty logs if they’re not critical.
    3. Consider splitting logs into multiple files or using log aggregation tools like ELK or Fluentd for archival.
    4. Check for log growth sources: du -sh /var/log/* to identify unusually large log directories.

Frequently Asked Questions

How do I set up log rotation for a specific application's logs?

Create a new file in /etc/logrotate.d/, add a template with rotation parameters (e.g., daily, rotate 7, compress), and include the application's log path. For example, for a service named myapp, use: /var/log/myapp.log { daily missingok rotate 7 compress notifempty create 640 myapp myapp }

What if I don’t want logs compressed?

Remove the compress and delaycompress directives. Logrotate will skip these options when not specified.

How can I manually trigger log rotation?

Use the logrotate command without arguments: sudo logrotate To force immediate rotation: sudo logrotate -f /etc/logrotate.conf

Why aren’t my rotated logs showing up?

Check for permissions, verify the log path in your configuration, and ensure create permissions are set correctly (e.g., create 640 myapp myapp). If the log file doesn’t exist, missingok will prevent logrotate from failing, but no rotation will occur.

Can I rotate logs for multiple services at once?

Yes, use sharedscripts in your logrotate configuration to run postrotate commands for all matching files. For example: /var/log/*.log { daily sharedscripts postrotate systemctl reload myapp.service myapp2.service endscript }

How do I troubleshoot logrotate not working?

Start with logrotate -v for verbose output. Check cron logs, verify file permissions, and ensure the configuration includes all specified paths. If logs still appear unrotated, test with -f or manually edit the config file.