Back to Blog
Tutorial

Cron Job Weekly: Schedule Tasks to Run Every Week (2025)

Cron Job Weekly: Schedule Tasks to Run Every Week (2025)

Need to run a task once per week? Weekly cron jobs are perfect for maintenance, backups, reports, and system updates.

This guide shows you how to set up weekly cron jobs with examples for every scenario.

Quick Answer: Weekly Cron Job (Sunday at Midnight)

0 0 * * 0 /path/to/your/script.sh

Breakdown:

  • 0 = Minute 0
  • 0 = Hour 0 (midnight)
  • * = Every day of month
  • * = Every month
  • 0 = Sunday (0 = Sunday, 1 = Monday... 6 = Saturday)

Result: Runs every Sunday at midnight


Alternative: Monday at Midnight

0 0 * * 1 /path/to/script.sh

Runs: Every Monday at 00:00


Days of Week in Cron

| Day | Number | Alternative | |-----|--------|-------------| | Sunday | 0 | 7 | | Monday | 1 | - | | Tuesday | 2 | - | | Wednesday | 3 | - | | Thursday | 4 | - | | Friday | 5 | - | | Saturday | 6 | - |

Note: Both 0 and 7 represent Sunday.


How to Set Up a Weekly Cron Job

Step 1: Open Your Crontab

crontab -e

Step 2: Add Your Weekly Job

# Weekly backup every Sunday at 2 AM
0 2 * * 0 /home/user/scripts/weekly-backup.sh

Step 3: Save and Exit

  • Nano: Ctrl+X, Y, Enter
  • Vim: Esc, :wq, Enter

Step 4: Verify

crontab -l

Weekly Cron Job Examples by Day

Sunday at 2 AM

0 2 * * 0 /path/to/script.sh

Monday at 9 AM

0 9 * * 1 /path/to/script.sh

Tuesday at 3 PM

0 15 * * 2 /path/to/script.sh

Wednesday at Noon

0 12 * * 3 /path/to/script.sh

Thursday at 6 PM

0 18 * * 4 /path/to/script.sh

Friday at 5 PM

0 17 * * 5 /path/to/script.sh

Saturday at 10 AM

0 10 * * 6 /path/to/script.sh

Real-World Weekly Cron Job Examples

Example 1: Weekly Full Database Backup

# Every Sunday at 2 AM
0 2 * * 0 /usr/bin/mysqldump -u root -pPASSWORD --all-databases > /backups/weekly/full-backup-$(date +\%Y-\%m-\%d).sql

What it does:

  • Full MySQL backup
  • Runs every Sunday
  • Uses date in filename
  • Stores in /backups/weekly/

Example 2: Weekly Server Updates (Debian/Ubuntu)

# Every Sunday at 3 AM
0 3 * * 0 apt-get update && apt-get upgrade -y >> /var/log/weekly-updates.log 2>&1

Example 3: Weekly Report Generation

# Every Monday at 9 AM
0 9 * * 1 /usr/bin/php /var/www/html/generate-weekly-report.php

Example 4: Clean Old Files Weekly

# Every Saturday at 4 AM
0 4 * * 6 find /tmp -type f -mtime +7 -delete

What it does:

  • Deletes files older than 7 days from /tmp
  • Runs every Saturday at 4 AM

Example 5: Weekly SSL Certificate Check

# Every Sunday at 2 AM
0 2 * * 0 certbot renew --quiet --deploy-hook "systemctl reload nginx"

Example 6: Weekly Analytics Report (Python)

# Every Monday at 8 AM
0 8 * * 1 cd /home/user/analytics && /home/user/analytics/venv/bin/python3 weekly_report.py

Example 7: Weekly Docker Cleanup

# Every Sunday at 3 AM
0 3 * * 0 docker system prune -af --volumes >> /var/log/docker-cleanup.log 2>&1

Example 8: Weekly Sitemap Generation

# Every Sunday at 5 AM
0 5 * * 0 cd /var/www/html && php artisan sitemap:generate

Advanced Weekly Scheduling

Every N Weeks

Note: Cron doesn't support "every N weeks" directly. Use workarounds:

Method 1: Using Week Number

# Every 2 weeks (even week numbers)
0 2 * * 0 [ $(($(date +\%U) \% 2)) -eq 0 ] && /path/to/script.sh

Method 2: Using Date Check

# Every 2 weeks starting from specific date
0 2 * * 0 [ $((($(date +\%s) - $(date -d "2025-01-01" +\%s)) / 604800 \% 2)) -eq 0 ] && /path/to/script.sh

First/Last Weekday of Month

First Monday of Each Month

0 9 * * 1 [ $(date +\%d) -le 7 ] && /path/to/script.sh

How it works:

  • Runs every Monday
  • Only executes if day of month is 1-7
  • Result: First Monday only

Last Friday of Month

0 17 * * 5 [ $(date -d "+7 days" +\%m) -ne $(date +\%m) ] && /path/to/script.sh

How it works:

  • Runs every Friday
  • Only if next week is a different month
  • Result: Last Friday only

Weekend vs Weekday Jobs

Weekends Only (Saturday & Sunday)

# Method 1: Both days at same time
0 10 * * 6,0 /path/to/script.sh

# Method 2: Using range (Sunday to Saturday wraps)
0 10 * * 0,6 /path/to/script.sh

Runs: 10 AM on Saturday and Sunday

Weekdays Only (Monday-Friday)

0 9 * * 1-5 /path/to/script.sh

Runs: 9 AM, Monday through Friday


Multiple Days Per Week

Monday, Wednesday, Friday

0 14 * * 1,3,5 /path/to/script.sh

Runs: 2 PM on Mon, Wed, Fri

Tuesday and Thursday

0 16 * * 2,4 /path/to/script.sh

Runs: 4 PM on Tue, Thu


Common Weekly Patterns

Weekly Backup Schedule

# Sunday: Full backup
0 2 * * 0 /scripts/full-backup.sh

# Monday-Saturday: Incremental backup (daily cron)
0 2 * * 1-6 /scripts/incremental-backup.sh

Weekly Maintenance Schedule

# Sunday 2 AM: Database backup
0 2 * * 0 /scripts/db-backup.sh

# Sunday 3 AM: System updates
0 3 * * 0 /scripts/system-update.sh

# Sunday 4 AM: Log cleanup
0 4 * * 0 /scripts/cleanup-logs.sh

# Sunday 5 AM: Generate reports
0 5 * * 0 /scripts/weekly-report.sh

Best Days for Weekly Tasks

| Day | Best For | Why? | |-----|----------|------| | Sunday | Full backups, system updates | Lowest traffic day | | Monday | Weekly reports, planning tasks | Start of business week | | Tuesday | Mid-week maintenance | Avoid Monday rush | | Wednesday | Data synchronization | Middle of week | | Thursday | Report preparation | Before end of week | | Friday | Cleanup, archiving | End of work week | | Saturday | Heavy processing | Weekend low traffic |


Common Mistakes & Fixes

❌ Wrong: Using Day Number 7 Instead of 0

0 2 * * 7 /script.sh  # Works but inconsistent

Better:

0 2 * * 0 /script.sh  # Standard Sunday representation

Both work, but 0 is more standard.

❌ Wrong: Expecting "Every 7 Days"

*/7 * * * * /script.sh  # This means every 7 minutes!

Correct:

0 0 * * 0 /script.sh  # Once per week on Sunday

❌ Wrong: Range Wrapping

0 0 * * 6-1 /script.sh  # Doesn't work as expected

Correct:

0 0 * * 6,0,1 /script.sh  # Saturday, Sunday, Monday

Or:

0 0 * * 0-1,6 /script.sh

Weekly Cron with Logging

Always log weekly tasks:

0 2 * * 0 /path/to/script.sh >> /var/log/weekly-cron.log 2>&1

Add timestamps:

0 2 * * 0 echo "[$(date)] Starting weekly backup" >> /var/log/weekly.log && /path/to/script.sh >> /var/log/weekly.log 2>&1

Weekly Backup Script Example

Create /home/user/scripts/weekly-backup.sh:

#!/bin/bash

# Weekly backup script
WEEK=$(date +%Y-W%U)
BACKUP_DIR="/backups/weekly"
LOG_FILE="/var/log/weekly-backup.log"

echo "[$(date)] Starting weekly backup for $WEEK" >> $LOG_FILE

# Create backup directory
mkdir -p $BACKUP_DIR

# Full database backup
mysqldump -u root -pPASSWORD --all-databases > $BACKUP_DIR/full-db-$WEEK.sql

# Backup entire website
tar -czf $BACKUP_DIR/website-$WEEK.tar.gz /var/www/html

# Backup configuration files
tar -czf $BACKUP_DIR/config-$WEEK.tar.gz /etc/nginx /etc/mysql /etc/php

# Delete backups older than 4 weeks (keep 1 month)
find $BACKUP_DIR -name "*.sql" -mtime +28 -delete
find $BACKUP_DIR -name "*.tar.gz" -mtime +28 -delete

# Upload to remote storage (optional)
# rsync -avz $BACKUP_DIR/ user@backup-server:/backups/

echo "[$(date)] Weekly backup completed!" >> $LOG_FILE

Make executable:

chmod +x /home/user/scripts/weekly-backup.sh

Add to crontab:

# Every Sunday at 2 AM
0 2 * * 0 /home/user/scripts/weekly-backup.sh

Email Notifications for Weekly Jobs

Send Email on Completion

MAILTO="admin@example.com"
0 2 * * 0 /path/to/weekly-backup.sh

Custom Email with Results

0 2 * * 0 /path/to/script.sh && echo "Weekly backup completed successfully" | mail -s "Weekly Backup Success" admin@example.com

Email Only on Failure

0 2 * * 0 /path/to/script.sh || echo "Weekly backup FAILED!" | mail -s "URGENT: Backup Failed" admin@example.com

Troubleshooting Weekly Cron Jobs

1. Job Didn't Run on Expected Day

Check system logs:

# Ubuntu/Debian
grep CRON /var/log/syslog | grep weekly

# CentOS/RHEL
grep weekly /var/log/cron

2. Wrong Day of Week

Verify day numbers:

date +%u  # Shows 1-7 (Monday-Sunday)
date +%w  # Shows 0-6 (Sunday-Saturday)

Cron uses: 0-6 (Sunday-Saturday)

3. Check Crontab Entry

crontab -l | grep weekly

4. Test Script Manually

/path/to/weekly-script.sh

5. Verify Script Permissions

ls -l /path/to/weekly-script.sh
chmod +x /path/to/weekly-script.sh

Time Zone for Weekly Jobs

Cron uses server timezone:

timedatectl

Change timezone:

sudo timedatectl set-timezone America/New_York

Or specify in crontab:

0 2 * * 0 TZ=America/Los_Angeles /path/to/script.sh

Preventing Overlapping Runs

If weekly job might take hours:

0 2 * * 0 flock -n /tmp/weekly-backup.lock -c '/path/to/backup.sh'

How it works:

  • Creates lock file
  • Skips if previous run still active
  • Prevents multiple instances

System-Wide Weekly Cron

Method 1: /etc/crontab

sudo nano /etc/crontab

Add:

0 2 * * 0 root /path/to/weekly-backup.sh

Method 2: /etc/cron.weekly/

Place script in /etc/cron.weekly/:

sudo nano /etc/cron.weekly/backup

Make executable:

sudo chmod +x /etc/cron.weekly/backup

Note: Runs via anacron, not at exact time.


Monitor Weekly Cron Jobs

Critical weekly tasks need monitoring:


Real-World Weekly Use Cases

1. Full Website Backup

# Every Sunday at 3 AM
0 3 * * 0 tar -czf /backups/website-$(date +\%Y-\%m-\%d).tar.gz /var/www

2. Database Optimization

# Every Sunday at 4 AM
0 4 * * 0 mysqlcheck -u root -pPASSWORD --optimize --all-databases

3. Security Updates

# Every Monday at 2 AM
0 2 * * 1 apt-get update && apt-get upgrade -y

4. Log Archiving

# Every Sunday at 1 AM
0 1 * * 0 tar -czf /backups/logs/logs-$(date +\%Y-\%m-\%d).tar.gz /var/log && find /var/log -name "*.log" -exec truncate -s 0 {} \;

5. Weekly Analytics Report

# Every Monday at 8 AM
0 8 * * 1 /usr/bin/python3 /home/user/scripts/generate-weekly-analytics.py

6. WordPress Updates

# Every Tuesday at 4 AM
0 4 * * 2 cd /var/www/html && wp core update --allow-root && wp plugin update --all --allow-root

7. Clear Redis Cache

# Every Sunday at midnight
0 0 * * 0 redis-cli FLUSHALL

8. Renew Let's Encrypt Certificates

# Every Sunday at 2 AM
0 2 * * 0 certbot renew --quiet --post-hook "systemctl reload nginx"

Performance Considerations

Stagger Weekly Tasks

# Sunday schedule - spread out tasks
0 1 * * 0 /scripts/task1.sh   # 1 AM
0 2 * * 0 /scripts/task2.sh   # 2 AM
0 3 * * 0 /scripts/task3.sh   # 3 AM
0 4 * * 0 /scripts/task4.sh   # 4 AM

Use Low Priority for Heavy Jobs

0 2 * * 0 nice -n 19 ionice -c3 /heavy-weekly-job.sh

Quick Reference: Weekly Cron Jobs

# Every Sunday at midnight
0 0 * * 0 /script.sh

# Every Monday at 9 AM
0 9 * * 1 /script.sh

# Every Friday at 5 PM
0 17 * * 5 /script.sh

# Weekends (Saturday & Sunday)
0 10 * * 6,0 /script.sh

# Weekdays (Monday-Friday)
0 9 * * 1-5 /script.sh

# Monday, Wednesday, Friday
0 14 * * 1,3,5 /script.sh

# First Monday of month
0 9 * * 1 [ $(date +\%d) -le 7 ] && /script.sh

# With logging
0 2 * * 0 /script.sh >> /var/log/weekly.log 2>&1

# Prevent overlap
0 2 * * 0 flock -n /tmp/weekly.lock /script.sh

# PHP
0 2 * * 0 /usr/bin/php /var/www/cron.php

# Python
0 2 * * 0 /usr/bin/python3 /home/user/script.py

# Node.js
0 2 * * 0 /usr/bin/node /home/user/app.js

Day of Week Quick Reference

# Sunday (both work)
0 0 * * 0 /script.sh
0 0 * * 7 /script.sh

# Monday
0 0 * * 1 /script.sh

# Tuesday
0 0 * * 2 /script.sh

# Wednesday
0 0 * * 3 /script.sh

# Thursday
0 0 * * 4 /script.sh

# Friday
0 0 * * 5 /script.sh

# Saturday
0 0 * * 6 /script.sh

Testing Weekly Cron Jobs

Method 1: Change to Tomorrow

# If today is Tuesday (day 2), set for Wednesday (day 3)
0 10 * * 3 /path/to/script.sh

Wait until tomorrow, verify, then set to weekly.

Method 2: Manual Test

/path/to/weekly-script.sh

Method 3: Temporary Daily Testing

# Test daily for a week
0 10 * * * /path/to/script.sh

Once verified, change to weekly:

0 10 * * 0 /path/to/script.sh

Disable/Remove Weekly Cron

Temporarily Disable

crontab -e

Comment out:

# 0 2 * * 0 /path/to/weekly-backup.sh

Permanently Remove

crontab -e

Delete the line.


Need Reliable Hosting?

Running weekly maintenance requires stable servers:


Comparison: Weekly vs Other Frequencies

| Frequency | Cron Expression | Runs Per Year | Best For | |-----------|----------------|---------------|----------| | Hourly | 0 * * * * | 8,760 | Monitoring, updates | | Daily | 0 2 * * * | 365 | Backups, cleanup | | Weekly | 0 2 * * 0 | 52 | Full backups, reports | | Monthly | 0 2 1 * * | 12 | Major maintenance |


Conclusion

Weekly cron jobs are perfect for regular maintenance:

# Every Sunday at 2 AM
0 2 * * 0 /path/to/weekly-script.sh

Key Takeaways:

  • ✅ Use day number (0-6 or 7 for Sunday)
  • ✅ Sunday is 0 (or 7)
  • ✅ Monday is 1, Saturday is 6
  • ✅ Comma for multiple days: 1,3,5
  • ✅ Range for consecutive: 1-5
  • ✅ Schedule during off-peak (weekends/nights)
  • ✅ Add logging for all weekly tasks
  • ✅ Use flock for long-running jobs

Next Steps:

  1. Create custom cron schedules
  2. Decode complex cron expressions
  3. Save and organize your cron jobs

Related Articles:

Keywords: cron job weekly, weekly cron, crontab weekly, run script weekly, linux weekly cron job, schedule weekly task, cron expression weekly, weekly backup cron, ubuntu weekly cron