正在加载,请稍候…

Cron Syntax Guide: How to Write Cron Expressions With Practical Examples

Master cron job syntax with this complete guide. Covers all five fields, special characters, named shortcuts, and 30+ real-world examples for scheduling tasks.

What Is Cron?

Cron is a time-based job scheduler in Unix/Linux systems. It runs commands or scripts at specified intervals — from every minute to once a year — without any manual trigger.

The schedule is defined by a cron expression: a string of 5 fields that describe when to run.

Cron Expression Structure

┌─────────── minute (0–59)
│ ┌───────── hour (0–23)
│ │ ┌─────── day of month (1–31)
│ │ │ ┌───── month (1–12 or JAN–DEC)
│ │ │ │ ┌─── day of week (0–7, where 0 and 7 = Sunday, or SUN–SAT)
│ │ │ │ │
* * * * *  command to execute

Each field can be:

  • * — any value ("every")
  • Number — specific value
  • a,b — list of values
  • a-b — range
  • */n — every n units (step)
  • a-b/n — range with step

Special Characters Explained

Character Example Meaning
* * * * * * Every minute
, 0 9,17 * * * At 9:00 and 17:00
- 0 9-17 * * * Every hour from 9 to 17
/ */15 * * * * Every 15 minutes
L 0 0 L * * Last day of month (some implementations)
? 0 0 ? * MON No specific value (Quartz scheduler)

Named Shortcuts

Many cron implementations support these convenient aliases:

Shortcut Expression Meaning
@yearly or @annually 0 0 1 1 * Once a year, Jan 1 at midnight
@monthly 0 0 1 * * Once a month, 1st at midnight
@weekly 0 0 * * 0 Once a week, Sunday at midnight
@daily or @midnight 0 0 * * * Once a day at midnight
@hourly 0 * * * * Once an hour
@reboot Once at startup

30+ Practical Cron Expression Examples

Every N minutes/hours

* * * * *          # Every minute
*/5 * * * *        # Every 5 minutes
*/10 * * * *       # Every 10 minutes
*/15 * * * *       # Every 15 minutes
*/30 * * * *       # Every 30 minutes
0 * * * *          # Every hour (at :00)
0 */2 * * *        # Every 2 hours
0 */6 * * *        # Every 6 hours
0 */12 * * *       # Every 12 hours

Specific times

0 0 * * *          # Every day at midnight
0 6 * * *          # Every day at 6:00 AM
30 8 * * *         # Every day at 8:30 AM
0 9-17 * * *       # Every hour from 9 AM to 5 PM
0 9,12,15 * * *    # At 9 AM, noon, and 3 PM
0 0 * * 0          # Every Sunday at midnight
0 0 * * 1          # Every Monday at midnight
0 0 * * 1-5        # Every weekday at midnight
0 0 * * 6,0        # Every weekend at midnight

Monthly and yearly

0 0 1 * *          # First day of every month at midnight
0 0 15 * *         # 15th of every month at midnight
0 0 1,15 * *       # 1st and 15th of every month
0 0 1 1 *          # January 1st (New Year)
0 0 1 */3 *        # First day of every quarter
0 0 L * *          # Last day of month (cron with L support)

Real-world task examples

# Backup database every day at 2 AM
0 2 * * * /scripts/backup-db.sh

# Clear temp files every Sunday at 3 AM
0 3 * * 0 find /tmp -mtime +7 -delete

# Send weekly report every Monday at 9 AM
0 9 * * 1 /scripts/send-weekly-report.sh

# Renew SSL certificate on the 1st and 15th at 3 AM
0 3 1,15 * * certbot renew --quiet

# Check disk space every 30 minutes
*/30 * * * * /scripts/check-disk.sh

# Run a Node.js script every 5 minutes during business hours
*/5 9-17 * * 1-5 node /app/sync-data.js

# Restart a service every night at 11 PM
0 23 * * * systemctl restart myapp

# Archive logs every month on the 1st at 1 AM
0 1 1 * * gzip /var/log/app/*.log

Setting Up Crontab

# Edit current user's crontab
crontab -e

# List current crontab
crontab -l

# Remove crontab
crontab -r

# Edit crontab for a specific user (as root)
crontab -u www-data -e

Example crontab file:

# Environment variables
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=admin@example.com

# Jobs
0 2 * * * /home/ubuntu/backup.sh >> /var/log/backup.log 2>&1
*/5 * * * * /home/ubuntu/health-check.sh
0 9 * * 1 /home/ubuntu/weekly-report.sh

Common Pitfalls

1. PATH issues

Cron runs with a minimal PATH. Scripts that work in your shell may fail in cron because commands aren't found.

Fix: Use full paths in your scripts:

# In crontab or at the top of your script:
PATH=/usr/local/bin:/usr/bin:/bin

# Or use absolute paths
/usr/bin/python3 /home/ubuntu/script.py

2. Missing output capture

Cron discards stdout/stderr by default unless MAILTO is set.

# Redirect output to a log file
0 2 * * * /scripts/backup.sh >> /var/log/backup.log 2>&1

# Suppress all output (use sparingly)
0 2 * * * /scripts/backup.sh > /dev/null 2>&1

3. Overlapping jobs

If a job takes longer than its interval, multiple instances can run simultaneously. Guard against this:

*/5 * * * * flock -n /tmp/myjob.lock /scripts/myjob.sh

4. Day-of-week numbering

Day 0 and day 7 are both Sunday. Day 1 is Monday. Double-check when using specific days.

Cron Alternatives

Tool Use Case
systemd timers Modern Linux, better logging
Kubernetes CronJob Container-based scheduled tasks
AWS EventBridge Cloud-native scheduling
GitHub Actions schedule CI/CD-based automation
APScheduler (Python) In-process scheduling
node-cron Node.js in-process scheduling

Frequently Asked Questions

Q: What's the minimum cron interval? 1 minute. Cron doesn't support sub-minute intervals. For frequent tasks, use a loop inside a long-running process.

Q: Does cron run when the computer is off? No. If the system is off during a scheduled time, that run is skipped. Use anacron for tasks that must run even after being missed.

Q: Why is my cron job not running? Check: cron service running (systemctl status cron), correct syntax, full paths in scripts, correct file permissions, and review /var/log/syslog for errors.

Q: How do I run a cron job in a specific timezone? Set TZ in the crontab: TZ=America/New_York before your jobs. Or use systemd timers which have native timezone support.

→ Generate cron expressions visually with the Crontab Generator.