TFT

CRON Expressions for Log Rotation & Cleanup

Automate system maintenance with CRON. Generate expressions for rotating logs, cleaning temp files, or clearing caches on a schedule. Keep your server tidy automatically.

CRON for Log Rotation & Cleanup
Templates for automated log rotation, temp file cleanup, and system maintenance
Configuration
Customize the templates for your environment
Daily Log Rotation
Rotate logs every day
0 0 * * *

Daily at midnight

High-volume applications with daily log cycles

Weekly Log Rotation
Rotate logs every week
0 0 * * 0

Every Sunday at midnight

Lower volume applications, weekly archives

Monthly Log Rotation
Rotate logs monthly
0 0 1 * *

1st of every month at midnight

Compliance archives, monthly reporting

Twice Daily Rotation
Rotate logs morning and evening
0 0,12 * * *

Midnight and noon daily

Very high-volume logging

Hourly Cleanup
Clean up old logs every hour
0 * * * *

Every hour

Strict disk space management

Temp File Cleanup
Remove temporary files daily
30 2 * * *

Daily at 2:30 AM

Temporary file management

Cache Cleanup
Clear cache directories weekly
0 3 * * 0

Every Sunday at 3:00 AM

Cache management and disk cleanup

Archive Old Logs
Archive logs older than 7 days
0 4 * * 1

Every Monday at 4:00 AM

Long-term log archival

Log Management Best Practices

Recommendations

  • • Rotate logs during low-traffic hours
  • • Compress old logs to save disk space
  • • Set appropriate retention periods
  • • Monitor disk space usage
  • • Use log aggregation for critical systems

Important Notes

  • • Test rotation scripts before deploying
  • • Ensure proper file permissions
  • • Consider application log reopening
  • • Archive logs before deletion for compliance
  • • Set up alerts for rotation failures

How CRON for Log Rotation Works

This tool provides CRON templates and shell scripts for automated log rotation, temporary file cleanup, and system maintenance tasks. Instead of writing rotation scripts from scratch, use proven templates that handle compression, retention, and archival.

Template Categories

  • Daily rotation: Rotate logs every day at midnight
  • Weekly rotation: Archive logs every Sunday
  • Monthly rotation: Create monthly log archives for compliance
  • Temp cleanup: Remove temporary files older than N days
  • Cache cleanup: Clear application caches on a schedule
  • Archive scripts: Move old logs to long-term storage

Who Needs Log Rotation Templates

System Administrators

Managing servers that generate gigabytes of logs daily. Prevent disk space exhaustion with automated rotation.

DevOps Engineers

Setting up logging infrastructure for containerized applications. Integrate with log aggregation systems.

Compliance Officers

Ensuring logs are retained for required periods (30, 90, 365 days) and archived properly for audits.

Application Developers

Building apps that write to log files. Provide operators with ready-to-use rotation configurations.

Database Administrators

Managing database query logs, slow query logs, and transaction logs that grow continuously.

Understanding Log Rotation Strategies

Rotation Frequency

Daily: Best for high-volume applications. Keeps individual log files manageable. Runs at midnight to capture a full day's activity in one file.

Weekly: Suitable for lower-traffic systems. Reduces the number of archived files. Typically runs Sunday morning when traffic is lowest.

Size-based: Rotate when logs exceed a threshold (e.g., 100MB). Useful for unpredictable traffic patterns. Requires monitoring rather than fixed schedules.

Compression Options

gzip: Standard compression, good balance of speed and size reduction. Files become .log.gz, typically 90% smaller than original.

delaycompress: Keep the most recent rotated log uncompressed. Useful when you need to tail or grep recent logs without decompressing.

No compression: Faster rotation, larger disk usage. Only use when disk space is abundant or logs need immediate processing.

Frequently Asked Questions

What's the difference between cron-based rotation and logrotate?

logrotate is a dedicated Linux utility with built-in rotation features (compression, email notifications, post-rotation scripts). Cron-based rotation uses shell scripts you control. Both can coexist—use logrotate for system logs and custom scripts for applications.

How do I handle applications that keep log files open?

Use the copytruncate method (copies then truncates the original) or send a signal to the application to reopen log files. Many services support SIGHUP or have a "reload" command that closes and reopens logs.

What retention period should I use?

It depends on your needs: 7-14 days for debugging recent issues, 30-90 days for operational analysis, 1+ years for compliance requirements. Consider storage costs versus the likelihood of needing old logs.

Should I rotate logs locally or send them to a central system?

For single servers, local rotation works fine. For distributed systems, use log shipping (Fluentd, Logstash, Filebeat) to send logs to a central system (Elasticsearch, Splunk) before rotation deletes them.

How do I test rotation scripts safely?

Run scripts manually first with a test directory. Use the -n (dry-run) flag if available. Test on a non-production server. Verify that applications continue logging after rotation.

Can I rotate logs based on both time and size?

Yes, combine conditions in your script. For example, rotate daily but also rotate immediately if the file exceeds 500MB. logrotate supports this with "maxsize" and "size" directives alongside time-based options.

logrotate vs Custom Scripts

Use logrotate When

  • Rotating standard system logs
  • You need built-in compression
  • Email notifications on rotation
  • Multiple applications share log directories
  • Standard rotation patterns suffice

Use Custom Scripts When

  • Complex rotation logic is needed
  • Uploading archives to cloud storage
  • Custom naming or directory structures
  • Integration with proprietary systems
  • Specific compliance requirements