Back to Home

Cron Expression: Every Sunday at Midnight (0 0 * * 0)

CronOS Team
cronschedulingweeklysundaytutorial

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 Sunday at Midnight (0 0 * * 0)

The cron expression 0 0 * * 0 executes a task every Sunday at midnight (00:00), making it ideal for weekly maintenance, reports, and tasks that should run at the start of each week.

Expression Breakdown

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

Field Values

FieldValueMeaning
Minute0At minute 0
Hour0At hour 0 (midnight)
Day of Month*Every day (1-31)
Month*Every month (1-12)
Day of Week0Sunday (0 or 7)

Note: In some systems, Sunday can be represented as 0 or 7. Both are valid.

Execution Time

This expression runs once per week at:

  • Sunday 00:00 (midnight)

Common Use Cases

1. Weekly Reports

bash
0 0 * * 0 /usr/bin/python3 /scripts/generate-weekly-report.py

Generate weekly analytics reports or summaries.

2. Weekly Backups

bash
0 0 * * 0 /usr/local/bin/weekly-backup.sh

Create full weekly backups of databases or systems.

3. Weekly Maintenance

bash
0 0 * * 0 /usr/local/bin/weekly-maintenance.sh

Run weekly database optimization, cleanup, or maintenance tasks.

4. Weekly Data Processing

bash
0 0 * * 0 /usr/bin/python3 /scripts/process-weekly-data.py

Process weekly data batches or aggregations.

Example Implementations

Weekly Report Script

python
# generate-weekly-report.py
import json
from datetime import datetime, timedelta
import sqlite3

def generate_weekly_report():
    conn = sqlite3.connect('/var/data/app.db')
    cursor = conn.cursor()
    
    # Get data from last week
    end_date = datetime.now()
    start_date = end_date - timedelta(days=7)
    
    cursor.execute('''
        SELECT 
            COUNT(*) as total_requests,
            AVG(response_time) as avg_response_time,
            COUNT(CASE WHEN status_code >= 400 THEN 1 END) as errors,
            COUNT(DISTINCT user_id) as unique_users
        FROM requests
        WHERE timestamp >= ? AND timestamp < ?
    ''', (start_date, end_date))
    
    metrics = cursor.fetchone()
    
    report = {
        'week_start': start_date.strftime('%Y-%m-%d'),
        'week_end': end_date.strftime('%Y-%m-%d'),
        'metrics': {
            'total_requests': metrics[0],
            'avg_response_time': round(metrics[1], 2) if metrics[1] else 0,
            'errors': metrics[2],
            'unique_users': metrics[3]
        }
    }
    
    with open(f'/var/reports/weekly_{start_date.strftime("%Y%m%d")}.json', 'w') as f:
        json.dump(report, f, indent=2)
    
    print(f"{datetime.now()}: Weekly report generated")
    conn.close()

if __name__ == '__main__':
    generate_weekly_report()

Best Practices

  1. Week Start: Sunday midnight marks the start of a new week
  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 weekly jobs
  5. Resource Management: Weekly jobs may be heavier, monitor resources

When to Use

Good for:

  • Weekly reports
  • Weekly backups
  • Weekly maintenance
  • Weekly data processing
  • Start-of-week operations

Avoid for:

  • Real-time critical operations
  • Tasks requiring more frequent execution

Related Patterns

PatternExpressionDescription
Every Sunday0 0 * * 0Start of week
Every Monday0 0 * * 1First weekday
Every Friday0 0 * * 5End of week

Conclusion

The 0 0 * * 0 expression is perfect for weekly operations that should run at the start of each week. It's commonly used for weekly reports, backups, and maintenance tasks, providing a clean weekly cycle for recurring operations.

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