Linux Administration·6 min read·

Logrotate Production Mastery: Size Limits, Compression, Signals

Master production log rotation on busy Linux nodes using copytruncate, postrotate daemon signaling, and zstd/gzip compression.

NB

Netbay Developer Relations

Netbay Engineering

On this page

Unchecked log files are among the most frequent causes of sudden disk full emergencies on production servers. When an application log exhausts storage, database engines trigger read-only emergency fallbacks, crash dumps fail to record, and system daemons terminate unpredictably. While logrotate is present on nearly every Linux distribution, naive default configurations frequently lose log lines during rotation or crash daemons due to improper signal handling.

Configuring logrotate for enterprise workloads requires mastering two core rotation mechanisms: the standard move-and-signal pattern (where the active file is renamed and the application receives a SIGHUP/SIGUSR1 to reopen its file descriptor), and copytruncate (used for legacy services that cannot reopen log handles).

Expand
Server Logs and Storage
Server Log Retention and Rotation
Production Logrotate Rotation & Compression Lifecycle Active File /var/log/app/app.log Size Threshold maxsize 500M trigger Atomic Rename app.log to app.log.1 Signal Daemon kill -USR1 or systemctl reload Async Zstandard Compress to .log.2.zst delaycompress ensures the newly created file is not locked or corrupted during active background writes.

High-Throughput Production Configuration

Here is an enterprise-ready logrotate configuration for an edge Nginx or reverse proxy service using modern Zstandard (zstd) compression:

ini
# /etc/logrotate.d/production-proxy
/var/log/proxy/*.log {
    daily
    missingok
    rotate 14
    maxsize 500M
    compress
    compresscmd /usr/bin/zstd
    compressext .zst
    compressoptions -19 -T0
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        if [ -f /var/run/nginx.pid ]; then
            kill -USR1 $(cat /var/run/nginx.pid)
        fi
    endscript
}

Key parameters in this configuration include:

  • maxsize 500M: Rotates immediately if the log exceeds 500MB, even if the daily cron has not yet elapsed.
  • compresscmd /usr/bin/zstd: Replaces legacy gzip with Zstandard, yielding up to 3x higher compression ratios at 5x the speed.
  • delaycompress: Delays compression until the subsequent rotation cycle, guaranteeing the active daemon has cleanly released the file descriptor.
  • sharedscripts: Executes the postrotate script once across all matching logs rather than repeatedly per file.

Testing and Debugging Rotations

Never deploy a logrotate rule without testing its execution path. Use the -d (debug) flag to simulate the run without touching any files on disk:

bash
# Dry run simulation with verbose evaluation output
sudo logrotate -d /etc/logrotate.d/production-proxy

# Force immediate execution to verify script execution and permissions
sudo logrotate -f /etc/logrotate.d/production-proxy

# Inspect the state file to review recorded rotation dates
cat /var/lib/logrotate/status | grep proxy

Automation and Scripting Video

Learn more about automating system maintenance tasks and Linux scripts in this overview:

Bash Automation and Linux Tooling in 100 Seconds
YouTube

Best Practices for Daemon Signals

When utilizing the postrotate block, always ensure the command returns exit code 0 even if the daemon is currently stopped. Using /bin/systemctl kill -s USR1 --kill-who=main proxy.service 2>/dev/null || true guarantees that transient service outages do not break the entire system-wide logrotate cron job.

Run reliable background jobs and log pipelines on rock-solid VPS infrastructure at netbayhosts.in.

Keep reading

Follow along on a real VPS

Deploy Linux in under 60 seconds

These guides are written against Ubuntu, Debian, and RHEL-family images — the same ones on NetBay.

Deploy an instance