Managing logs effectively is critical for system stability and security. Uncontrolled logs can quickly fill your storage and impact performance. This guide will show you how to automate daily log rotation using logrotate and cron in a reliable, industry-standard way.
- Logrotate: Mature, widely supported tool designed specifically to manage log files, ensuring logs stay manageable and disk usage remains predictable
- Cron: Simple, proven scheduling utility in Linux, making it ideal for automating periodic tasks.
- You have administrative access (sudo privileges) to your Linux system.
- You are using a Debian/Ubuntu or RHEL/CentOS Linux distribution.
- Your logs are stored under
/var/log/moderntechops/
. - Your system has cron installed and properly configured.
sudo mkdir -p /var/log/moderntechops
sudo touch /var/log/moderntechops/example.log
You can even add some text to simulate real logs:
echo "Test log entry: $(date)" | sudo tee -a /var/log/moderntechops/example.log


logrotate --version
If not installed, you can add it easily:
- Debian/Ubuntu:
sudo apt update
sudo apt install logrotate
- RHEL/CentOS:
sudo yum install logrotate

sudo nano /etc/logrotate.d/moderntechops
Add the following content, adjusting paths and log files accordingly:
/var/log/moderntechops/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0640 root adm
sharedscripts
postrotate
systemctl reload rsyslog > /dev/null 2>&1 || true
endscript
}
daily
: Rotate logs daily.rotate 7
: Retain logs for 7 days.compress
: Compress older logs to save space.delaycompress
: Delay compression until the next rotation.missingok
: Avoid errors if logs are missing.notifempty
: Don’t rotate empty files.create
: Creates new logs with specified permissions and ownership.sharedscripts
: Runs scripts only once per rotation cycle.postrotate
: Reloads rsyslog after rotation (modify for your logging service).

sudo logrotate --debug /etc/logrotate.conf
Ensure no errors or warnings appear.


sudo crontab -e
Add the following line:
45 18 * * * /usr/sbin/logrotate /etc/logrotate.conf
┌─────────────── Minute (30)
│ ┌───────────── Hour (18 = 6 PM in 24-hour format)
│ │ ┌─────────── Day of Month (* = every day)
│ │ │ ┌───────── Month (* = every month)
│ │ │ │ ┌─────── Day of Week (* = every day of the week)
│ │ │ │ │
30 18 * * * /usr/sbin/logrotate /etc/logrotate.conf
45
= minute (30 minutes past the hour)18
= 6 PM in 24-hour format (6 PM = 18:00)
Save and exit.


ls -lh /var/log/moderntechops/
You should see compressed .gz
archives from past rotations and today’s active log.

- Simplicity: Minimal configuration and easy maintenance.
- Stability: Mature tools with extensive documentation and community support.
- Efficiency: Lightweight operations that won’t noticeably affect server performance.
- Compatibility: Widely available on most Linux distributions by default.
- Systemd timers: Modern alternative to cron. Good for systemd-heavy environments but less universal and familiar than cron.
- Custom Bash scripts: Offer total flexibility but require manual maintenance and potential debugging overhead.
Given the above, logrotate
with cron provides a balanced, reliable solution widely adopted across industries.
- Linux Manual. (n.d.). Logrotate Manual. Retrieved from https://man7.org/linux/man-pages/man8/logrotate.8.html (“A detailed manual describing logrotate’s options and capabilities.”)
- Linux Manual. (n.d.). Cron Job Scheduling. Retrieved from https://man7.org/linux/man-pages/man5/crontab.5.html (“Comprehensive explanation of cron syntax and usage.”)
- CETS UPenn. (n.d.). Linux System Administration Best Practices. Retrieved from https://cets.seas.upenn.edu/answers/linux-best-practices.html (“Guidance covering critical Linux administration practices including logging, security, and backups.”)
Last updated: June 9, 2025