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.
0 0 * * *Daily at midnight
High-volume applications with daily log cycles
0 0 * * 0Every Sunday at midnight
Lower volume applications, weekly archives
0 0 1 * *1st of every month at midnight
Compliance archives, monthly reporting
0 0,12 * * *Midnight and noon daily
Very high-volume logging
0 * * * *Every hour
Strict disk space management
30 2 * * *Daily at 2:30 AM
Temporary file management
0 3 * * 0Every Sunday at 3:00 AM
Cache management and disk cleanup
0 4 * * 1Every Monday at 4:00 AM
Long-term log archival
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
Other Free Tools
CRON Expression Generator
Free CRON Expression Generator
CRON Expression Validator & Explainer
Validate & Understand CRON Expressions
CRON to English Translator
CRON to English Translator
English to CRON Expression Converter
English to CRON Expression Converter
CRON Expression Tester (Next Run Times)
CRON Expression Tester - See Next Run Times
ASCII to Hex Converter
ASCII to Hex Converter: Text to Hexadecimal Translator
Barcode Generator
Free Barcode Generator
Binary to Text Converter
Binary to Text Converter
Free Printable Calendar Maker
Create & Print Your Custom Calendar
Pie Chart Maker
Free Pie Chart Maker Online