🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

logrotate Command

Intermediate Logging & Monitoring man(1)

Rotate, compress, and manage log files

👁 10 views 📅 Updated: Mar 15, 2026
SYNTAX
logrotate [OPTION]... CONFIG

What Does logrotate Do?

logrotate manages log file rotation, compression, and deletion. It prevents log files from growing indefinitely and consuming all disk space by automatically rotating, compressing, and removing old log files.

logrotate runs daily via cron/systemd timer. It reads configuration from /etc/logrotate.conf and /etc/logrotate.d/ to determine how each log should be handled.

logrotate supports rotation by size, time, or count. It can compress old logs, run scripts before/after rotation, and handle various rotation strategies.

Options & Flags

OptionDescriptionExample
-d Debug mode (dry run) logrotate -d /etc/logrotate.conf
-f Force rotation sudo logrotate -f /etc/logrotate.d/nginx
-v Verbose output logrotate -v /etc/logrotate.conf
-s Use alternate state file logrotate -s /tmp/logrotate.state /etc/logrotate.conf

Practical Examples

#1 Force rotation

Forces immediate rotation of nginx logs.
$ sudo logrotate -f /etc/logrotate.d/nginx

#2 Test configuration

Shows what would happen without actually rotating.
$ logrotate -d /etc/logrotate.d/myapp

#3 Custom config example

Rotates daily, keeps 30 days, compresses, and reloads the service.
$ /var/log/myapp/*.log {\n daily\n rotate 30\n compress\n delaycompress\n missingok\n notifempty\n create 0644 www-data www-data\n postrotate\n systemctl reload myapp\n endscript\n}

#4 Verbose run

Runs logrotate with verbose output showing all actions.
$ sudo logrotate -v /etc/logrotate.conf

#5 Check status

Shows when each log was last rotated.
$ cat /var/lib/logrotate/status

Tips & Best Practices

Always test with -d: logrotate -d config tests your configuration without making changes. Always verify before deploying.
Configuration directives: daily/weekly/monthly for frequency. rotate N keeps N old files. compress uses gzip. missingok ignores missing files.
postrotate scripts: Many services need HUP/reload after rotation: postrotate systemctl reload nginx endscript. Without this, the service writes to the old (now renamed) file.

Frequently Asked Questions

How do I set up log rotation for my app?
Create /etc/logrotate.d/myapp with rotation rules. Test with logrotate -d /etc/logrotate.d/myapp.
Why are my logs not being rotated?
Check: Is the config in /etc/logrotate.d/? Is the path correct? Run logrotate -d to debug. Check /var/lib/logrotate/status.
How do I force immediate rotation?
sudo logrotate -f /etc/logrotate.d/config_name forces rotation regardless of schedule.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →