Back to Home

Cron Expression: Every Day at 2:00 AM (0 2 * * *)

CronOS Team
cronschedulingdailyearly-morningtutorial

Need to generate a cron expression?

Use CronOS to generate any cron expression you wish with natural language. Simply describe what you need, and we'll create the perfect cron expression for you. It's completely free!

Generate Cron Expression

Cron Expression: Every Day at 2:00 AM (0 2 * * *)

The cron expression 0 2 * * * executes a task every day at 2:00 AM, making it ideal for early morning maintenance, backups, and data processing tasks during low-traffic hours.

Expression Breakdown

bash
0 2 * * *
│ │ │ │ │
│ │ │ │ └─── Day of week: * (every day)
│ │ │ └───── Month: * (every month)
│ │ └─────── Day of month: * (every day)
│ └───────── Hour: 2 (at hour 2, 2:00 AM)
└─────────── Minute: 0 (at minute 0)

Field Values

FieldValueMeaning
Minute0At minute 0
Hour2At hour 2 (2:00 AM)
Day of Month*Every day (1-31)
Month*Every month (1-12)
Day of Week*Every day of week (0-7)

Execution Time

This expression runs once per day at:

  • 02:00 (2:00 AM)

Common Use Cases

1. Early Morning Backups

bash
0 2 * * * /usr/local/bin/early-backup.sh

Create backups during early morning low-traffic hours.

2. Database Maintenance

bash
0 2 * * * /usr/local/bin/db-maintenance.sh

Run database optimization, vacuum, or index maintenance.

3. Log Processing

bash
0 2 * * * /usr/bin/python3 /scripts/process-logs.py

Process and analyze log files from the previous day.

4. Cache Refresh

bash
0 2 * * * /usr/bin/python3 /scripts/refresh-cache.py

Refresh cached data, computed statistics, or pre-warm cache.

Example Implementations

Early Morning Backup Script

bash
#!/bin/bash
# /usr/local/bin/early-backup.sh

BACKUP_DIR="/var/backups/early"
SOURCE_DIR="/var/data"
TIMESTAMP=$(date +%Y%m%d)
LOG_FILE="/var/log/backups.log"

mkdir -p $BACKUP_DIR

tar -czf "$BACKUP_DIR/backup_$TIMESTAMP.tar.gz" \
    -C $(dirname $SOURCE_DIR) \
    $(basename $SOURCE_DIR) >> $LOG_FILE 2>&1

find $BACKUP_DIR -name "*.tar.gz" -mtime +30 -delete

echo "$(date): Early morning backup completed" >> $LOG_FILE

Database Maintenance Script

bash
#!/bin/bash
# /usr/local/bin/db-maintenance.sh

DB_NAME="app_db"
DB_USER="dbuser"
LOG_FILE="/var/log/db-maintenance.log"

# PostgreSQL maintenance
psql -U $DB_USER -d $DB_NAME -c "VACUUM ANALYZE;" >> $LOG_FILE 2>&1

# MySQL maintenance (alternative)
# mysql -u $DB_USER -p$DB_PASS $DB_NAME -e "OPTIMIZE TABLE important_table;" >> $LOG_FILE 2>&1

echo "$(date): Database maintenance completed" >> $LOG_FILE

Best Practices

  1. System Load: 2 AM is typically a low-traffic period
  2. Error Handling: Implement comprehensive error handling and logging
  3. Locking: Use file locks to prevent concurrent execution
  4. Monitoring: Set up alerts for failed jobs
  5. Resource Management: Monitor CPU, memory, and I/O usage

When to Use

Good for:

  • Early morning backups
  • Database maintenance
  • Log processing
  • Cache refresh
  • Heavy data processing

Avoid for:

  • Real-time critical operations
  • Tasks requiring immediate execution

Related Patterns

PatternExpressionDescription
Daily at 1 AM0 1 * * *One hour earlier
Daily at 2 AM0 2 * * *Early morning
Daily at 6 AM0 6 * * *Pre-dawn

Conclusion

The 0 2 * * * expression is perfect for early morning maintenance tasks. It's commonly used for backups, database maintenance, and data processing during low-traffic hours when system resources are more available.

Need to generate a cron expression?

Use CronOS to generate any cron expression you wish with natural language. Simply describe what you need, and we'll create the perfect cron expression for you. It's completely free!

Generate Cron Expression