Linux Tutorials

Cron Jobs & Scheduling

Automate tasks with crontab. Learn syntax, examples, shortcuts, logging, and troubleshooting.

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

┌───────────── 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

Example: Weekly backup

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

Editing the Crontab

crontab -e edits your personal schedule. System-wide jobs live in /etc/crontab or the hourly/daily directories.

Built-in Time Shortcuts

@reboot    # run once at startup
@hourly    # every hour
@daily     # once a day at midnight
@weekly    # once a week
@monthly   # once a month
@yearly    # once a year

Common Examples

0 2 * * * find /tmp -type f -atime +7 -delete    # clear tmp files daily 2 AM
0 3 * * 0 tar czf /backups/home-$(date +%F).tgz /home/username   # backup weekly
*/15 * * * * rsync -av --delete ~/projects/ remote:/backup/projects/   # sync every 15 min

Environment Variables

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

If a command isn’t in PATH, cron won’t find it. MAILTO defines who receives job output.

Redirecting Output & Logging

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

Separate logs:

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

Email Notifications

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

Suppress emails with:

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

Troubleshooting & Tips

grep CRON /var/log/syslog   # check system cron logs
crontab -l | crontab -      # validate crontab