Setting Up Log Rotation with logrotate

Setting Up Log Rotation with logrotate

Without log rotation, log files grow indefinitely and can fill your disk. logrotate automatically archives and deletes old logs.

How logrotate Works

logrotate is typically run daily by cron. It reads config files in /etc/logrotate.d/ and rotates matching log files based on your rules.

View Existing Configs

ls /etc/logrotate.d/
cat /etc/logrotate.d/nginx

Create a Custom Config

nano /etc/logrotate.d/myapp
/var/log/myapp/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        systemctl reload myapp > /dev/null 2>&1 || true
    endscript
}

Key Directives Explained

  • daily / weekly / monthly — rotation frequency
  • rotate 7 — keep 7 rotated files before deleting
  • compress — gzip old log files
  • delaycompress — compress previous rotation, not the current one (helps apps still writing)
  • missingok — do not error if the log file is missing
  • notifempty — do not rotate empty log files
  • postrotate — command to run after rotation (e.g., send HUP to reopen log files)

Test Your Config

logrotate -d /etc/logrotate.d/myapp

The -d flag runs in debug/dry-run mode — shows what would happen without actually rotating.

Force a Rotation Now

logrotate -f /etc/logrotate.d/myapp
  • 0 Els usuaris han Trobat Això Útil
Ha estat útil la resposta?

Articles Relacionats

Managing Users and Groups

Managing Users and Groups on Linux Proper user management is essential for server security. Avoid...

Managing Services with systemctl

Managing Services with systemctl systemctl is the standard tool for managing services on modern...

Monitoring Server Resources

Monitoring Server Resources Keeping an eye on your server's resource usage helps you identify...

Scheduling Tasks with Cron

Scheduling Tasks with Cron Cron allows you to schedule commands to run automatically at set...

Installing and Configuring Nginx

Installing and Configuring Nginx Nginx is a high-performance web server and reverse proxy widely...