Back to Blog
Tutorial

Cron Job Daily: How to Schedule Tasks to Run Every Day (2025)

Cron Job Daily: How to Schedule Tasks to Run Every Day (2025)

Need to automate a task that runs once per day? Daily cron jobs are essential for backups, reports, maintenance, and cleanup tasks.

This comprehensive guide shows you how to set up cron jobs that run daily with practical examples for every scenario.

Quick Answer: Daily Cron Job at Midnight

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

Breakdown:

  • 0 = Minute 0
  • 0 = Hour 0 (midnight)
  • * = Every day of month
  • * = Every month
  • * = Every day of week

Result: Runs once per day at 00:00 (midnight)


Alternative Daily Times

Daily at 2 AM (Recommended for Backups)

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

Daily at 9 AM (Business Hours)

0 9 * * * /path/to/report.sh

Daily at 6 PM

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

Daily at Noon

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

How to Set Up a Daily Cron Job

Step 1: Open Your Crontab

crontab -e

First time? Choose your editor (nano is easiest for beginners).

Step 2: Add Your Daily Job

0 2 * * * /home/user/scripts/daily-backup.sh

Step 3: Save and Exit

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

Step 4: Verify Installation

crontab -l

You should see your new daily cron job listed.


Real-World Daily Cron Job Examples

Example 1: MySQL Database Backup (Daily at 2 AM)

0 2 * * * /usr/bin/mysqldump -u root -pPASSWORD mydb > /backups/daily/mydb-$(date +\%Y-\%m-\%d).sql

What it does:

  • Creates MySQL backup
  • Runs daily at 2 AM
  • Uses date in filename (2025-01-15.sql)
  • Stores in /backups/daily/

Example 2: PostgreSQL Backup

0 2 * * * /usr/bin/pg_dump -U postgres mydb > /backups/postgres-$(date +\%Y-\%m-\%d).sql

Example 3: Log Cleanup (Delete Old Logs)

0 3 * * * find /var/log/myapp -name "*.log" -mtime +30 -delete

What it does:

  • Runs daily at 3 AM
  • Deletes log files older than 30 days
  • Frees disk space automatically

Example 4: Daily Report Generation (PHP)

0 8 * * * /usr/bin/php /var/www/html/generate-daily-report.php

Runs: 8 AM daily

Example 5: Python Data Analytics

0 1 * * * cd /home/user/analytics && /home/user/analytics/venv/bin/python3 daily_analysis.py

Best practice: Always cd to project directory first

Example 6: Website Sitemap Generation

0 4 * * * cd /var/www/html && php artisan sitemap:generate

Runs: 4 AM daily (off-peak hours)

Example 7: Node.js Email Digest

0 7 * * * /usr/bin/node /home/user/app/send-daily-digest.js >> /var/log/digest.log 2>&1

Runs: 7 AM daily with logging

Example 8: Docker Container Cleanup

0 4 * * * docker system prune -af --volumes >> /var/log/docker-cleanup.log 2>&1

What it does:

  • Removes unused containers, images, volumes
  • Runs at 4 AM daily
  • Logs output for review

Weekdays vs Weekends

Weekdays Only (Monday-Friday)

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

Runs: 9 AM, Monday through Friday only

Use cases:

  • Business reports
  • Office hours tasks
  • Weekday notifications

Weekends Only (Saturday-Sunday)

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

Or:

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

Runs: 10 AM on Saturday and Sunday

Specific Days

# Monday only
0 9 * * 1 /path/to/monday-report.sh

# Friday only
0 17 * * 5 /path/to/friday-cleanup.sh

# Wednesday and Friday
0 14 * * 3,5 /path/to/script.sh

Multiple Times Per Day

Twice Daily (8 AM and 8 PM)

0 8,20 * * * /path/to/script.sh

Three Times Daily (9 AM, 1 PM, 5 PM)

0 9,13,17 * * * /path/to/script.sh

Every 12 Hours

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

Runs at: 00:00 and 12:00

Every 6 Hours

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

Runs at: 00:00, 06:00, 12:00, 18:00


Best Times to Schedule Daily Cron Jobs

| Time | Use Case | Why? | |------|----------|------| | 00:00 (Midnight) | Log rotation, cache clearing | Day boundary, low traffic | | 02:00 (2 AM) | Database backups | Minimal user activity | | 03:00 (3 AM) | System maintenance | Off-peak hours | | 04:00 (4 AM) | Cleanup tasks | Low server load | | 07:00 (7 AM) | Email digests | Before work hours | | 08:00 (8 AM) | Reports generation | Start of business day | | 12:00 (Noon) | Mid-day sync | Lunch break | | 18:00 (6 PM) | End-of-day reports | After business hours | | 23:00 (11 PM) | Pre-midnight tasks | Before daily rollover |


Common Daily Cron Job Patterns

Daily at Midnight

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

Daily at 2:30 AM

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

Daily at 9:15 AM

15 9 * * * /path/to/script.sh

Daily at 3:45 PM

45 15 * * * /path/to/script.sh

Daily at 11:59 PM (Before Midnight)

59 23 * * * /path/to/script.sh

Advanced Daily Scheduling

First Day of Month Only

0 2 1 * * /path/to/monthly-report.sh

Runs: 2 AM on the 1st of each month

Last Day of Month

0 2 28-31 * * [ $(date -d tomorrow +\%d) -eq 1 ] && /path/to/script.sh

Specific Date (January 1st)

0 0 1 1 * /path/to/new-year-task.sh

Every 15th of the Month

0 2 15 * * /path/to/mid-month-task.sh

Common Mistakes & Fixes

❌ Wrong: Missing Time Specification

* * * * * /script.sh  # Runs EVERY MINUTE!

Correct:

0 0 * * * /script.sh  # Runs once daily at midnight

❌ Wrong: Using 24 for Midnight

0 24 * * * /script.sh  # INVALID! Hours are 0-23

Correct:

0 0 * * * /script.sh  # Midnight is hour 0

❌ Wrong: Relative Paths

0 2 * * * ~/backup.sh  # Won't work in cron

Correct:

0 2 * * * /home/user/backup.sh  # Use absolute paths

❌ Wrong: Using AM/PM

0 2PM * * * /script.sh  # Invalid syntax

Correct:

0 14 * * * /script.sh  # Use 24-hour format (14 = 2 PM)

❌ Wrong: Forgetting Script Permissions

0 2 * * * /path/to/script.sh  # Won't run if not executable

Fix:

chmod +x /path/to/script.sh

Add Logging to Daily Cron Jobs

Always log your daily cron jobs for debugging:

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

Redirect explanation:

  • >> = Append to log file
  • 2>&1 = Redirect errors to same log

View logs:

tail -f /var/log/daily-cron.log

View last 100 lines:

tail -100 /var/log/daily-cron.log

Daily Backup Script Example

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

#!/bin/bash

# Daily backup script
DATE=$(date +%Y-%m-%d)
BACKUP_DIR="/backups/daily"
LOG_FILE="/var/log/daily-backup.log"

echo "[$(date)] Starting daily backup..." >> $LOG_FILE

# Create backup directory
mkdir -p $BACKUP_DIR

# Backup MySQL database
mysqldump -u root -pPASSWORD mydb > $BACKUP_DIR/mydb-$DATE.sql

# Backup files
tar -czf $BACKUP_DIR/files-$DATE.tar.gz /var/www/html

# Delete backups older than 7 days
find $BACKUP_DIR -name "*.sql" -mtime +7 -delete
find $BACKUP_DIR -name "*.tar.gz" -mtime +7 -delete

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

Make it executable:

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

Add to crontab:

0 2 * * * /home/user/scripts/daily-backup.sh

Email Notifications

Send Email on Completion

MAILTO="admin@example.com"
0 2 * * * /path/to/script.sh

Cron automatically emails output if MAILTO is set.

Send Email Only on Error

0 2 * * * /path/to/script.sh || echo "Daily backup failed!" | mail -s "ERROR" admin@example.com

Send Success Email

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

Troubleshooting Daily Cron Jobs

1. Job Not Running at All

Check if cron service is running:

sudo systemctl status cron

Start if stopped:

sudo systemctl start cron

Enable on boot:

sudo systemctl enable cron

2. Check Cron Logs

Ubuntu/Debian:

grep CRON /var/log/syslog | tail -50

CentOS/RHEL:

tail -50 /var/log/cron

3. Test Script Manually

/path/to/your/script.sh

If it works manually but not in cron, it's likely an environment issue.

4. Environment Variables

Cron has a minimal environment. Set variables explicitly:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SHELL=/bin/bash

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

5. Check Script Permissions

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

6. Verify Crontab Syntax

crontab -l

Look for syntax errors.


Time Zone Considerations

Cron uses the server's system time zone.

Check current timezone:

timedatectl

Change timezone (example: New York):

sudo timedatectl set-timezone America/New_York

Or use specific timezone in script:

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

System-Wide Daily Cron

Method 1: /etc/crontab

sudo nano /etc/crontab

Add:

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

Note: Must specify username (root).

Method 2: /etc/cron.daily/

Place executable script in /etc/cron.daily/:

sudo nano /etc/cron.daily/my-backup

Make executable:

sudo chmod +x /etc/cron.daily/my-backup

Note: Runs via anacron, not at exact time.


Monitor Daily Cron Jobs

For critical daily tasks, use monitoring:


Daily Cron vs Alternatives

Cron (Traditional)

Pros:

  • ✅ Built into Linux
  • ✅ Simple syntax
  • ✅ No dependencies

Cons:

  • ❌ No retry on failure
  • ❌ Minimal logging
  • ❌ Skips missed runs

Systemd Timers

sudo systemctl edit --force --full daily-backup.timer
[Unit]
Description=Daily Backup Timer

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Pros:

  • ✅ Better logging (journalctl)
  • ✅ Persistent (runs missed jobs)
  • ✅ Dependencies support

Cons:

  • ❌ More complex setup
  • ❌ Requires systemd

Real-World Daily Cron Use Cases

1. Website Daily Backup

0 3 * * * /usr/bin/tar -czf /backups/website-$(date +\%Y-\%m-\%d).tar.gz /var/www/html

2. SSL Certificate Renewal Check

0 2 * * * /usr/bin/certbot renew --quiet

3. Clear Cache Daily

0 0 * * * rm -rf /var/cache/myapp/*

4. Daily Sales Report

0 7 * * * cd /var/www && php artisan reports:daily-sales

5. Sync Files to Remote Server

0 4 * * * rsync -avz /var/www/html/ user@backup-server:/backups/website/

6. Update Exchange Rates

0 8 * * * curl https://api.exchangerate.com/latest > /var/www/data/rates.json

7. Generate Daily Sitemap

0 5 * * * cd /var/www && npm run generate-sitemap

8. Restart Service Daily (Clear Memory Leaks)

0 4 * * * sudo systemctl restart myapp

Performance Best Practices

1. Avoid Peak Hours

Don't schedule resource-intensive tasks during peak traffic:

# ❌ Bad: Runs during business hours
0 14 * * * /heavy-processing.sh

# ✅ Good: Runs during off-peak
0 3 * * * /heavy-processing.sh

2. Stagger Multiple Daily Jobs

# Spread out to avoid overload
0 2 * * * /backup-database.sh
0 3 * * * /cleanup-logs.sh
0 4 * * * /generate-reports.sh

3. Use Nice/Ionice for Low Priority

0 2 * * * nice -n 19 ionice -c3 /low-priority-task.sh

4. Prevent Overlapping Runs

0 2 * * * flock -n /tmp/daily-backup.lock -c '/backup.sh'

Quick Reference: Daily Cron Jobs

# Daily at midnight
0 0 * * * /script.sh

# Daily at 2 AM
0 2 * * * /script.sh

# Daily at 9 AM
0 9 * * * /script.sh

# Daily at 6 PM
0 18 * * * /script.sh

# Weekdays only (Mon-Fri) at 9 AM
0 9 * * 1-5 /script.sh

# Weekends only at 10 AM
0 10 * * 6,7 /script.sh

# First day of month
0 2 1 * * /script.sh

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

# With email on error
0 2 * * * /script.sh || mail -s "Error" admin@example.com

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

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

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

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

24-Hour Time Conversion

| 12-Hour | 24-Hour | Cron Hour | |---------|---------|-----------| | 12:00 AM (Midnight) | 00:00 | 0 | | 1:00 AM | 01:00 | 1 | | 2:00 AM | 02:00 | 2 | | 6:00 AM | 06:00 | 6 | | 9:00 AM | 09:00 | 9 | | 12:00 PM (Noon) | 12:00 | 12 | | 1:00 PM | 13:00 | 13 | | 3:00 PM | 15:00 | 15 | | 6:00 PM | 18:00 | 18 | | 9:00 PM | 21:00 | 21 | | 11:00 PM | 23:00 | 23 |


Need Reliable Hosting for Daily Cron Jobs?


Disable/Remove Daily Cron Job

Temporarily Disable

crontab -e

Comment out:

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

Permanently Remove

crontab -e

Delete the line completely.

Remove All Cron Jobs

crontab -r

Warning: This deletes ALL your cron jobs!


Testing Daily Cron Jobs

Method 1: Run Immediately

Change to run in next minute:

# If current time is 14:25, set to 14:26
26 14 * * * /path/to/script.sh

Watch it execute, verify, then change to daily schedule.

Method 2: Manual Execution

/path/to/script.sh

Method 3: Temporary Frequent Testing

# Test every minute
* * * * * /path/to/script.sh

Verify it works, then change to daily:

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

Conclusion

Setting up daily cron jobs is essential for automation:

0 2 * * * /path/to/your/daily-script.sh

Key Takeaways:

  • ✅ Use 0 0 * * * for midnight
  • ✅ Use 0 2 * * * for 2 AM (recommended for backups)
  • ✅ Always use absolute paths
  • ✅ Add logging for debugging
  • ✅ Test manually first
  • ✅ Use 1-5 for weekdays only
  • ✅ Schedule during off-peak hours
  • ✅ Use flock to prevent overlaps

Next Steps:

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

Related Articles:

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