Cron Jobs & Scheduling

What is Cron?

Cron is the built-in Linux scheduler that runs scripts or commands automatically at specified times or recurring intervals without manual intervention.

Cron Syntax

Crontab format

Each line in your crontab follows:

┌───────────── minute (0–59)
│ ┌───────────── hour (0–23)
│ │ ┌───────────── day of month (1–31)
│ │ │ ┌───────────── month (1–12)
│ │ │ │ ┌───────────── day of week (0–7, Sun=0 or 7)
│ │ │ │ │
* * * * *  command to execute
  • Minute: 0–59
  • Hour: 0–23
  • Day of Month: 1–31
  • Month: 1–12
  • Day of Week: 0–7 (Sunday = 0 or 7)

Example: Weekly backup

45 4 * * 1 /usr/local/bin/weekly-backup.sh

This runs at 04:45 every Monday:

  • 45 → minute
  • 4 → hour
  • * → any day-of-month
  • * → any month
  • 1 → Monday

Omitting Fields with “*”

Use * to match “every” value in that position.

30 * * * * /usr/local/bin/hourly-report.sh

This runs at the 30-minute mark of every hour, because the hour field is *.

Example: Monthly cleanup on 1st at midnight

0 0 1 * * /usr/local/bin/monthly-cleanup.sh

Here:

  • 0 → minute
  • 0 → hour
  • 1 → day-of-month (1st)
  • * → any month
  • * → any day-of-week

Editing the Crontab

User-specific jobs

Run crontab -e to edit your personal schedule. Each user’s crontab is stored separately.

System-wide jobs

Place scripts in /etc/cron.hourly, /etc/cron.daily, or edit /etc/crontab for system tasks.

Built-in Time Shortcuts

Use special strings instead of five fields:

@reboot    # run once at startup
@hourly    # every hour
@daily     # once a day at midnight
@weekly    # once a week (Sun 00:00)
@monthly   # once a month (1st 00:00)
@yearly    # once a year (Jan 1 00:00)

Common Examples

Clear temp files daily at 2 AM

0 2 * * * /usr/bin/find /tmp -type f -atime +7 -delete

Backup home every Sunday at 3 AM

0 3 * * 0 tar czf /backups/home-$(date +%F).tgz /home/username

Sync project folder every 15 minutes

*/15 * * * * rsync -av --delete ~/projects/ remote:/backup/projects/

Using sleep Within Jobs

You can introduce delays between commands in a single cron entry:

0 1 * * * /path/to/task.sh; sleep 300; /path/to/cleanup.sh

Example

30 4 * * * echo "Start"; sleep 60; echo "End"

Environment Variables

Cron uses a minimal shell environment. Set variables at top of your crontab:

PATH=/usr/local/bin:/usr/bin:/bin
SHELL=/bin/bash
MAILTO=admin@example.com

Why it matters

If a command isn’t in default PATH, cron won’t find it unless you export it here.

Redirecting Output & Logging

By default, cron emails any stdout/stderr. To log to a file instead:

0 0 * * * /path/to/script.sh >> /var/log/myscript.log 2>&1

Example: separate logs

5 6 * * * /backup.sh > /var/log/backup.out 2> /var/log/backup.err

Email Notifications

Set MAILTO in crontab to receive output via email:

MAILTO="ops@example.com"
0 7 * * * /opt/daily-report.sh

Suppress email

Redirect both outputs to /dev/null:

15 8 * * * /cleanup.sh > /dev/null 2>&1

Troubleshooting & Tips

Check system cron logs

grep CRON /var/log/syslog

Validate your crontab

crontab -l | crontab -

Any syntax error will be reported when reloading.