bashbeginner

Logrotate Configuration for Applications

Logrotate config for application logs with daily rotation, compression, and post-rotate hooks.

bash
# /etc/logrotate.d/webapp
/var/log/webapp/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 webapp webapp
    sharedscripts
    postrotate
        systemctl reload webapp 2>/dev/null || true
    endscript
}

# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
    endscript
}

# Test configuration
# sudo logrotate -d /etc/logrotate.d/webapp
# Force rotation
# sudo logrotate -f /etc/logrotate.d/webapp

Use Cases

  • Preventing disk space exhaustion from logs
  • Maintaining log history with compression
  • Graceful log rotation for running services

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.