Back to Home

Cron Expression: Every Day at 8 AM and 8 PM (0 8,20 * * *)

CronOS Team
cronschedulingdailytwice-dailytutorial

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 8 AM and 8 PM (0 8,20 * * *)

The cron expression 0 8,20 * * * executes a task every day at 8:00 AM and 8:00 PM (20:00), making it ideal for twice-daily operations, reports, and tasks that should run at the start and end of the business day.

Expression Breakdown

bash
0 8,20 * * *
│ │   │ │ │
│ │   │ │ └─── Day of week: * (every day)
│ │   │ └───── Month: * (every month)
│ │   └─────── Day of month: * (every day)
│ └─────────── Hour: 8,20 (8 AM and 8 PM)
└───────────── Minute: 0 (at minute 0)

Field Values

FieldValueMeaning
Minute0At minute 0
Hour8,20Hours 8 and 20 (8 AM and 8 PM)
Day of Month*Every day (1-31)
Month*Every month (1-12)
Day of Week*Every day of week (0-7)

List Syntax

The 8,20 in the hour field is a list that means "hour 8 and hour 20":

  • Runs at: 08:00 (8:00 AM) and 20:00 (8:00 PM)
  • Total: 2 executions per day

Execution Time

This expression runs 2 times per day at:

  • 08:00 (8:00 AM) and 20:00 (8:00 PM)

Common Use Cases

1. Twice-Daily Reports

bash
0 8,20 * * * /usr/bin/python3 /scripts/generate-twice-daily-report.py

Generate reports at the start and end of the business day.

2. Twice-Daily Backups

bash
0 8,20 * * * /usr/local/bin/twice-daily-backup.sh

Create backups at the start and end of each day.

3. Twice-Daily Data Sync

bash
0 8,20 * * * /usr/bin/python3 /scripts/sync-twice-daily-data.py

Sync data from external systems or APIs twice daily.

4. Twice-Daily Cache Refresh

bash
0 8,20 * * * /usr/bin/python3 /scripts/refresh-cache.py

Refresh cached data or computed values twice daily.

Example Implementations

Twice-Daily Report Script

python
# generate-twice-daily-report.py
import json
from datetime import datetime
import sqlite3

def generate_twice_daily_report():
    conn = sqlite3.connect('/var/data/app.db')
    cursor = conn.cursor()
    
    current_hour = datetime.now().hour
    period = "morning" if current_hour == 8 else "evening"
    
    # Get data based on period
    if period == "morning":
        # Get yesterday's data
        yesterday = datetime.now() - timedelta(days=1)
        start_date = yesterday.replace(hour=0, minute=0, second=0)
        end_date = yesterday.replace(hour=23, minute=59, second=59)
    else:
        # Get today's data so far
        today = datetime.now()
        start_date = today.replace(hour=0, minute=0, second=0)
        end_date = today
    
    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
        FROM requests
        WHERE timestamp >= ? AND timestamp <= ?
    ''', (start_date, end_date))
    
    metrics = cursor.fetchone()
    
    report = {
        'period': period,
        'date': datetime.now().strftime('%Y-%m-%d'),
        'time': datetime.now().strftime('%H:%M:%S'),
        'metrics': {
            'total_requests': metrics[0],
            'avg_response_time': round(metrics[1], 2) if metrics[1] else 0,
            'errors': metrics[2]
        }
    }
    
    with open(f'/var/reports/{period}_{datetime.now().strftime("%Y%m%d_%H")}.json', 'w') as f:
        json.dump(report, f, indent=2)
    
    print(f"{datetime.now()}: {period.capitalize()} report generated")
    conn.close()

if __name__ == '__main__':
    generate_twice_daily_report()

Best Practices

  1. Twice-Daily Timing: 8 AM and 8 PM provide good start/end points
  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. Performance: Optimize tasks to complete efficiently

When to Use

Good for:

  • Twice-daily reports
  • Twice-daily backups
  • Twice-daily data sync
  • Start/end of day operations
  • Twice-daily cache refresh

Avoid for:

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

Related Patterns

PatternExpressionDescription
Twice daily0 8,20 * * *8 AM and 8 PM
Daily at 8 AM0 8 * * *Once daily at 8 AM
Daily at 8 PM0 20 * * *Once daily at 8 PM

Conclusion

The 0 8,20 * * * expression is perfect for twice-daily operations. It's commonly used for reports, backups, and data synchronization that should run at the start and end of each day, providing a good balance between keeping systems updated and managing resource usage.

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