When running web applications, enterprise software, or database systems on Linux servers, application logs record crucial diagnostic details, user transactions, and system activity. However, if these log files are left unmanaged, they expand indefinitely over time. A server disk filled to 100% capacity leads to sudden database corruption, application failure, and complete service downtime. For businesses in Pakistan relying on reliable server infrastructure for daily operations, implementing an automated log management strategy is an essential maintenance task.
Log rotation is the process of archiving, compressing, and eventually deleting old log files on a scheduled basis. Linux operating systems use a utility named logrotate to automate this workflow. In this tutorial, NetBit provides a step-by-step guide on how to configure standard log rotation rules for your business application servers to maintain optimal performance and storage balance.
Understanding the Role of Logrotate on Business Servers
The logrotate daemon runs daily as a scheduled system task (via cron or systemd timers). It inspects configured directories, checks file sizes or creation dates against administrator rules, and performs four major operational actions:
- Rotation: Renames the active log file (e.g.,
app.logbecomesapp.log.1) and creates a fresh, empty log file for ongoing writes. - Compression: Compresses older rotated files using gzip format (e.g.,
app.log.2.gz) to save up to 90% of disk space. - Retention: Retains a specified number of historical archive files before deleting the oldest entries.
- Post-Processing: Triggers application reloads or process signals to ensure active web servers (like Nginx or Apache) resume writing into the new log file without restarting the entire server.
Step 1: Auditing Your Current Log Disk Usage
Before modifying system configuration files, inspect your server's current log storage usage to identify high-volume log directories. Connect to your server terminal via SSH and check overall disk space:
df -h
Next, check the exact storage consumption inside the standard log directory and any custom application directories:
sudo du -sh /var/log/*
sudo du -sh /var/www/*/logs
If custom software platforms (such as Laravel, Node.js, or Python applications) write log output to custom directory paths, note these file paths down. You will configure specific rotation rules for these custom directories in the next steps.
Step 2: Exploring the Main Configuration Structure
In modern Linux server deployments (such as Ubuntu Server or Debian), global settings reside in /etc/logrotate.conf. Individual service configurations are stored in separate files inside the /etc/logrotate.d/ directory.
View the global defaults using a text reader:
cat /etc/logrotate.conf
You will see global options such as weekly rotation, log creation permissions, and standard compression rules. Rather than editing the main global file directly, best practice requires creating dedicated individual configuration files for each business application inside /etc/logrotate.d/.
Step 3: Creating a Custom Application Rotation Rule
Suppose your business runs a web application located at /var/www/business_app/logs/production.log. Create a new logrotate configuration file for this application using your preferred editor:
sudo nano /etc/logrotate.d/business_app
Paste the following complete configuration block into the file:
/var/www/business_app/logs/*.log {
daily
rotate 14
missingok
notifempty
compress
delaycompress
create 0640 www-data www-data
sharedscripts
postrotate
systemctl reload nginx > /dev/null 2>&1 || true
endscript
}
Understanding Each Rule Directive:
- daily: Rotates the application logs once every 24 hours. Alternative values include
weeklyormonthly. - rotate 14: Retains 14 compressed backup files before deleting older archives. This maintains two full weeks of historical diagnostic history.
- missingok: Prevents the system from throwing fatal error notifications if a log file is temporarily missing.
- notifempty: Instructs logrotate to skip running the rotation process if the log file contains zero bytes.
- compress & delaycompress: Enables gzip compression, but delays compressing the most recent rotated file (
app.log.1) until the following cycle. This ensures active processes finish writing buffered lines safely. - create 0640 www-data www-data: Automatically constructs a fresh, empty log file after rotation with read/write permissions for the server web user (
www-data). - postrotate / endscript: Executes custom terminal commands immediately after rotating files, such as reloading Nginx to update file descriptors.
Step 4: Rotating Logs Based on File Size Thresholds
If your business application processes heavy traffic, rotating purely on a daily schedule may still leave disk drives vulnerable during high-activity spikes. You can configure rotation based on absolute file size limits instead of time frames.
Modify your rule block to include the maxsize directive:
/var/log/custom_service/*.log {
size 100M
rotate 10
compress
missingok
notifempty
create 0644 root root
}
In this example, rotation triggers as soon as any single log file reaches 100 Megabytes, keeping total disk usage strictly capped at approximately 1 Gigabyte across 10 retained rotations.
Step 5: Testing Your Logrotate Rules (Dry Run)
Never apply new server maintenance configurations without validating syntax first. Execute a manual dry run using the debug switch (-d) to test how logrotate interprets your configuration without altering files:
sudo logrotate -d /etc/logrotate.d/business_app
Review the printed output terminal messages carefully. The debug report confirms which files were detected, whether permissions match, and if rotation would occur under current server conditions.
To force an immediate, live rotation test without waiting for the scheduled cron job, run:
sudo logrotate -f /etc/logrotate.d/business_app
Check your storage directory with ls -l /var/www/business_app/logs/ to confirm that compressed .gz archive files have been generated correctly with correct ownership flags.
Frequently Asked Questions
How often does logrotate run on standard Linux servers?
On most Linux distributions, logrotate is triggered once daily by the system's automated job scheduler (cron or systemd timer). It usually runs during early morning low-traffic hours (between 3:00 AM and 6:00 AM local time).
Will log rotation cause application downtime?
No. When configured correctly using signal commands or light process reloads (such as systemctl reload nginx), log rotation occurs seamlessly in the background without dropping active user connections or interrupting application services.
What happens if disk space reaches 100% before logrotate executes?
If a partition becomes completely full, system processes cannot open new file descriptors, causing application crashes. This is why combining logrotate with proactive disk space alerting or configuring lower file size rotation thresholds (size 50M) is critical for enterprise environments.
Partner with NetBit for Enterprise Server Solutions in Pakistan
Managing Linux infrastructure, configuring daemon automation, and preventing unexpected server outages requires precise system administration expertise. Based in Khuzdar, Pakistan, netbit provides specialized server management, custom web application architecture, network setup, and IT support designed for modern Pakistani organizations.
Whether your business needs local enterprise server deployment, custom backend application development, or proactive system maintenance, our local engineering team at netbit khuzdar delivers robust, scalable solutions tailored to your operational needs. Contact netbit tech today to evaluate your server setup and optimize your technical infrastructure for peak performance.
Reader comments.
Share your question, suggestion or experience related to this article.
No comments yet
There are no approved comments on this article yet. You can be the first person to leave one.