Back to Home

Cron Expression: Every Weekday at Midnight (0 0 * * 1-5)

CronOS Team
cronschedulingweeklyweekdaystutorial

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 Weekday at Midnight (0 0 * * 1-5)

The cron expression 0 0 * * 1-5 executes a task every weekday (Monday through Friday) at midnight (00:00), making it ideal for business-day operations, daily maintenance, and tasks that should run only on workdays.

Expression Breakdown

bash
0 0 * * 1-5
│ │ │ │ │
│ │ │ │ └─── Day of week: 1-5 (Monday to Friday)
│ │ │ └───── 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 Week1-5Monday through Friday

Range Syntax

The 1-5 in the day of week field is a range that means "Monday through Friday":

  • Runs on: Monday, Tuesday, Wednesday, Thursday, Friday
  • Does NOT run on: Saturday, Sunday

Execution Time

This expression runs 5 times per week at:

  • Monday 00:00, Tuesday 00:00, Wednesday 00:00, Thursday 00:00, Friday 00:00

Common Use Cases

1. Business Day Backups

bash
0 0 * * 1-5 /usr/local/bin/daily-backup.sh

Create daily backups only on business days.

2. Business Day Reports

bash
0 0 * * 1-5 /usr/bin/python3 /scripts/generate-daily-report.py

Generate daily reports only on workdays.

3. Business Day Maintenance

bash
0 0 * * 1-5 /usr/local/bin/daily-maintenance.sh

Run daily maintenance tasks only on weekdays.

4. Business Day Data Processing

bash
0 0 * * 1-5 /usr/bin/python3 /scripts/process-daily-data.py

Process daily data batches only on business days.

Example Implementations

Business Day Backup Script

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

BACKUP_DIR="/var/backups/daily"
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): Business day backup completed" >> $LOG_FILE

Business Day Report Script

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

def generate_daily_report():
    conn = sqlite3.connect('/var/data/app.db')
    cursor = conn.cursor()
    
    # Get yesterday's data
    yesterday = datetime.now() - timedelta(days=1)
    
    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 DATE(timestamp) = DATE(?)
    ''', (yesterday,))
    
    metrics = cursor.fetchone()
    
    report = {
        'date': yesterday.strftime('%Y-%m-%d'),
        'day_type': 'business_day',
        '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/daily_{yesterday.strftime("%Y%m%d")}.json', 'w') as f:
        json.dump(report, f, indent=2)
    
    print(f"{datetime.now()}: Business day report generated")
    conn.close()

if __name__ == '__main__':
    generate_daily_report()

Best Practices

  1. Business Days Only: This pattern excludes weekends, ideal for business operations
  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 system resources during execution

When to Use

Good for:

  • Business day backups
  • Business day reports
  • Business day maintenance
  • Business day data processing
  • Tasks that should only run on workdays

Avoid for:

  • Tasks that need to run every day including weekends
  • Real-time critical operations

Related Patterns

PatternExpressionDescription
Every weekday at midnight0 0 * * 1-5Monday-Friday at midnight
Every weekday at 8 AM0 8 * * 1-5Monday-Friday at 8 AM
Every weekend0 0 * * 6,0Saturday and Sunday

Conclusion

The 0 0 * * 1-5 expression is perfect for tasks that should run only on business days. It's commonly used for daily backups, reports, and maintenance tasks that are specific to workdays, excluding weekends from the schedule.

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