Skip to content

How to Schedule Tasks with Cron Jobs (Linux)

Cron is the Linux task scheduler. It runs commands or scripts at specified times, dates, or intervals automatically.

Crontab Syntax

A cron job line has five time fields followed by the command:

* * * * * command
─ ─ ─ ─ ─
│ │ │ │ │
│ │ │ │ └── Day of week (0-7, 0=Sunday)
│ │ │ └──── Month (1-12)
│ │ └────── Day of month (1-31)
│ └──────── Hour (0-23)
└────────── Minute (0-59)

Common Examples

# Every day at 2:30 AM
30 2 * * * /home/user/backup.sh

# Every hour
0 * * * * /usr/bin/php /var/www/html/cron.php

# Every Monday at 9 AM
0 9 * * 1 /scripts/weekly-report.sh

# First day of every month at midnight
0 0 1 * * /scripts/monthly-cleanup.sh

# Every 15 minutes
*/15 * * * * /scripts/check-status.sh

# Every 5 minutes during business hours (9 AM - 5 PM)
*/5 9-17 * * 1-5 /scripts/notify.sh

# Twice a day (6 AM and 6 PM)
0 6,18 * * * /scripts/daily-digest.sh

Special Syntax

@reboot     # Runs once at system startup
@yearly     # 0 0 1 1 * (January 1st)
@monthly    # 0 0 1 * * (First of every month)
@weekly     # 0 0 * * 0 (Sunday)
@daily      # 0 0 * * * (Midnight)
@hourly     # 0 * * * * (Top of every hour)

Editing Crontab

crontab -e          # edit your cron jobs (opens in default editor)
crontab -l          # list your current cron jobs
crontab -r          # remove all cron jobs (careful!)

Viewing Cron Logs

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

# RHEL / Fedora
grep CRON /var/log/cron

# Check if a specific job ran
grep "backup.sh" /var/log/syslog

Common Pitfalls

1. Use absolute paths

Cron runs with a minimal environment. Paths may not include /usr/local/bin:

# Bad
*/5 * * * * python script.py

# Good
*/5 * * * * /usr/bin/python3 /home/user/script.py

2. Redirect output

Cron emails you if a command produces output. Redirect to a log file:

* * * * * /home/user/script.sh >> /var/log/script.log 2>&1

3. Environment variables

Cron doesn’t load your profile. Set PATH manually:

PATH=/usr/bin:/usr/local/bin:/home/user/.local/bin
*/5 * * * * mycommand

System Crontab

System-wide cron jobs go in /etc/crontab (requires sudo):

# /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin

# Run weekly maintenance every Sunday at 3 AM
0 3 * * 0 root /usr/local/bin/maintenance.sh

Crontab Generator Tools

  • crontab.guru — best online tool for checking cron expressions
  • crontab-generator.org — visual crontab builder

Related: Learn Linux file permissions and our command cheat sheet.