正在加载,请稍候…

Crontab Syntax Explained: Schedule Cron Jobs Like a Pro

Master crontab scheduling syntax with our visual generator. Learn how to write cron expressions for any schedule.

What Is a Cron Job?

A cron job is a scheduled task that runs automatically at specified intervals on Unix-like operating systems. The name comes from "chronos" (Greek for time). Cron is the system service; crontab (cron table) is the file where scheduled tasks are defined.

Cron is used for: automated backups, database maintenance, sending scheduled emails, generating reports, cleaning temporary files, updating caches, and any repetitive task that needs to run without human intervention.

The Crontab Syntax

A crontab entry has six fields:

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

Special Characters

Character Meaning Example
* Any value (wildcard) * * * * * runs every minute
, Value list 1,15,30 means at 1st, 15th, 30th
- Range 9-17 means 9am to 5pm
/ Step values */15 means every 15 units

Common Cron Schedule Examples

# Every minute
* * * * * /path/to/script.sh

# Every hour at minute 0
0 * * * * /path/to/script.sh

# Every day at midnight
0 0 * * * /path/to/script.sh

# Every day at 2:30 AM
30 2 * * * /path/to/script.sh

# Every Monday at 9 AM
0 9 * * 1 /path/to/script.sh

# Every weekday at 6 PM
0 18 * * 1-5 /path/to/script.sh

# Every 15 minutes
*/15 * * * * /path/to/script.sh

# First day of every month at midnight
0 0 1 * * /path/to/script.sh

# Every Sunday at 3 AM (for weekly maintenance)
0 3 * * 0 /path/to/backup.sh

# Twice daily at 8 AM and 8 PM
0 8,20 * * * /path/to/script.sh

Special Predefined Schedules

Most cron implementations support shorthand keywords:

Keyword Equivalent Description
@reboot (none) Once at startup
@yearly 0 0 1 1 * Once a year (Jan 1 midnight)
@annually 0 0 1 1 * Same as @yearly
@monthly 0 0 1 * * Once a month (1st, midnight)
@weekly 0 0 * * 0 Once a week (Sunday midnight)
@daily 0 0 * * * Once a day (midnight)
@midnight 0 0 * * * Same as @daily
@hourly 0 * * * * Once an hour

Managing Crontab

# View current user's crontab
crontab -l

# Edit crontab in default editor
crontab -e

# Remove all cron jobs for current user
crontab -r

# Edit another user's crontab (requires root)
sudo crontab -u username -e

# List another user's crontab
sudo crontab -u username -l

Cron Environment and Common Pitfalls

Cron jobs run in a minimal environment - the PATH, HOME, and other variables you rely on in your shell may not be available.

PATH issues: Specify full paths to executables:

# Bad - 'python' may not be found
0 2 * * * python /opt/scripts/backup.py

# Good - use full path
0 2 * * * /usr/bin/python3 /opt/scripts/backup.py

Output redirection: By default, cron emails output to the system user. Redirect output to a log file:

0 2 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1

Timezone: Cron uses the system timezone. If your server is in UTC but you want jobs at "local" times, account for the offset.

Missed jobs: If the server is down when a job should run, the job is skipped - it does not catch up when the server restarts (unless you use anacron).

Using This Tool

Enter your desired schedule in plain English or fill in the time fields, and the tool generates the correct crontab expression. You can also paste a crontab expression to see a human-readable description.

-> Try the Crontab Generator