SyntaxStudy
Sign Up
Linux / Bash Crontab Syntax and Scheduling Jobs
Linux / Bash Beginner 1 min read

Crontab Syntax and Scheduling Jobs

Cron is the classic Unix job scheduler that executes commands on a fixed time schedule. The cron daemon reads schedule definitions from crontab files and runs the specified commands at the configured times. Each user can have their own crontab, and a system-wide crontab at `/etc/crontab` exists for administrative tasks. A crontab entry consists of five time fields followed by the command to execute. The fields are: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-7, where both 0 and 7 represent Sunday). Each field accepts a specific value, a wildcard `*` (any value), a range `1-5`, a step value `*/15` (every 15 units), or a comma-separated list `1,15,30`. Special strings like `@reboot`, `@daily`, `@weekly`, and `@monthly` are shorthand for common schedules. Cron jobs run in a minimal environment without your shell startup files, so environment variables like `PATH` may not be set as expected. Best practice is to use full absolute paths for both the command and any files it accesses, or explicitly set `PATH` at the top of the crontab.
Example
# Edit your user crontab
crontab -e

# List your crontab entries
crontab -l

# Remove your crontab
crontab -r

# Edit another user's crontab (root only)
sudo crontab -u alice -e

# ---- Crontab syntax ----
# m  h  dom  mon  dow  command
# |  |   |    |    |
# |  |   |    |    +--- Day of week (0-7, Sun=0 or 7)
# |  |   |    +-------- Month (1-12)
# |  |   +------------- Day of month (1-31)
# |  +----------------- Hour (0-23)
# +-------------------- Minute (0-59)

# Run every minute
# * * * * * /path/to/command

# Run at 2:30 AM every day
# 30 2 * * * /usr/bin/backup.sh

# Run every 15 minutes
# */15 * * * * /usr/bin/health_check.sh

# Run at 8 AM on weekdays (Mon-Fri)
# 0 8 * * 1-5 /usr/bin/morning_report.sh

# Run on the 1st and 15th of every month at midnight
# 0 0 1,15 * * /usr/bin/billing.sh

# Run every Sunday at 3 AM
# 0 3 * * 0 /usr/bin/weekly_cleanup.sh

# Special strings
# @reboot   /usr/bin/start_service.sh
# @daily    /usr/bin/daily_backup.sh
# @weekly   /usr/bin/weekly_task.sh
# @monthly  /usr/bin/monthly_report.sh
# @hourly   /usr/bin/hourly_check.sh

# Redirect output (cron mails output by default)
# 30 2 * * * /usr/bin/backup.sh >> /var/log/backup.log 2>&1

# Set PATH inside crontab
# PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin