Mastering Logrotate: Efficient Log Management for Linux Servers
Learn how to use logrotate to manage server logs, automate compression, and prevent disk overflow. Keep your Linux storage healthy with these best practices.

Previously in this course, we covered Project Task: Opening Server Ports for Your Web Server, ensuring our web server is reachable. In this lesson, we address a critical operational reality: as your web server handles traffic, it generates logs. If left unmanaged, these logs will eventually consume all available disk space, leading to system instability. We will use logrotate to automate the cleanup and retention of these files.
Understanding Logrotate and Disk Management
In Linux, services write events to text files. Over time, these files grow indefinitely. logrotate is the standard system utility designed to prevent these files from filling up your partitions. It handles the "rotation" process: renaming the current log, creating a new empty one, compressing the old file, and eventually deleting it after a set number of cycles.
Proper disk management via logrotate ensures your server remains performant and predictable. Without it, you risk a "disk full" scenario where your database might crash or your web server might stop writing logs entirely.
How Logrotate Works

logrotate is typically executed by cron (which you learned about in Introduction to Cron Jobs: A Guide to Linux Task Automation). It uses a configuration file, usually /etc/logrotate.conf, and directory-specific configs in /etc/logrotate.d/.
When a rotation triggers, the utility follows a specific lifecycle:
- Rotate: Rename the current log (e.g.,
access.logbecomesaccess.log.1). - Compress: Optionally zip the older log to save space.
- Post-rotate: Send a signal to the application (like Nginx or Apache) to reopen the new log file so it doesn't keep writing to the renamed file.
Worked Example: Configuring a Custom Log Policy
For our running project, let's assume our web application writes logs to /var/log/mywebapp/access.log. We want to keep 7 days of logs, compress them, and rotate them daily.
First, create a configuration file in /etc/logrotate.d/:
Bashsudo nano /etc/logrotate.d/mywebapp
Add the following configuration block:
TEXT/var/log/mywebapp/*.log { daily missingok rotate 7 compress delaycompress notifempty create 0640 www-data adm }
Breakdown of the Configuration:
- daily: Rotates the log every day.
- missingok: Doesn't throw an error if the log file is missing.
- rotate 7: Keeps 7 rotated files before deleting the oldest.
- compress: Uses gzip to compress old logs.
- delaycompress: Postpones compression until the next rotation cycle (useful if the application needs a moment to finish writing).
- notifempty: Skips rotation if the file is empty.
- create 0640 www-data adm: Creates a new log file with specific permissions and ownership after rotating the old one.
Hands-on Exercise
To verify your configuration works without waiting for a full day, you can run logrotate in "debug" or "force" mode.
- Verify the syntax: Run the command in debug mode to see what would happen:
sudo logrotate -d /etc/logrotate.d/mywebapp - Force a rotation: If you are confident, force the rotation now:
sudo logrotate -f /etc/logrotate.d/mywebapp - Verify results: Use
ls -l /var/log/mywebapp/to see if the new file was created and the old one was renamed.
Common Pitfalls
- Permission Denied: If the user running
logrotatedoesn't have write access to the log directory, the rotation will fail. Ensure thecreatedirective matches the user/group that owns the service. - Forgetting to notify the application: Some applications hold an open file handle to the log file. If you move the file without telling the application, it will keep writing to the renamed file. If your application doesn't automatically reopen logs, use the
postrotatescript block in your config to send a signal, such assystemctl reload myapp. - Over-aggressive rotation: Be careful with
rotatecounts. If you rotate hourly and keep only 10, you might lose vital debugging data from a weekend incident.
FAQ
Q: Does logrotate restart my web server?
A: Not necessarily. It usually just signals the application to "reopen" its log files. Using systemctl reload is the preferred way to signal most modern services.
Q: Where are the logs kept?
A: By default, in /var/log/. Always ensure your specific project directories are owned by the correct user.
Q: Is logrotate safe to run manually?
A: Yes, using the -f (force) flag, though it's best to test with -d (debug) first to ensure your regex or pathing is correct.
Recap
We've implemented a robust disk management strategy using logrotate. By automating the rotation, compression, and cleanup of our project logs, we ensure our server storage remains stable. This is a vital step in maintaining Log-File Analysis for Search Engine Behavior: A Pro Guide data and general system health.
Up next: We will dive into Shell Scripting Logic, where we'll learn to add decision-making capabilities to our automated maintenance tasks.
Work with me

VPS Server Setup, Deployment & Hardening
Get your app live on a fast, secure server — properly configured, hardened, and deployment-ready. No more wrestling with the command line.

CI/CD Pipeline & Docker Containerization
Ship with confidence: automated CI/CD pipelines and Docker setups so every push is tested and deployed — no more manual, error-prone releases.

