Crontab Syntax Explained: The Complete Guide to Cron Jobs
Master crontab syntax with clear examples. Learn the five-field cron expression format, special characters, common schedules, and how to debug cron jobs.
Understanding the Five-Field Format
A cron expression consists of five fields separated by spaces: 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 specific values, ranges, lists, and step values. For example, '30 9 * * 1-5' means 'at 9:30 AM every weekday.' The asterisk wildcard matches all valid values for that field. Reading left to right: minute 30, hour 9, any day of month, any month, Monday through Friday. Use a crontab guru tool to validate your expressions before deploying them.
Special Characters and Advanced Patterns
Beyond basic values, cron supports powerful special characters. The comma creates lists: '1,15' in the day field means the 1st and 15th. The hyphen creates ranges: '9-17' in the hour field means 9 AM through 5 PM. The slash defines step values: '*/5' in the minute field means every 5 minutes. The 'L' character (in some implementations) means the last day: 'L' in the day-of-month field triggers on the last day of each month. Combine these for complex schedules: '0 */2 * * 1-5' runs every 2 hours on weekdays, and '15,45 9-17 * * *' runs at 15 and 45 minutes past each hour from 9 AM to 5 PM.
Common Cron Schedule Examples
Every minute: '* * * * *'. Every hour at minute zero: '0 * * * *'. Daily at midnight: '0 0 * * *'. Every Monday at 8 AM: '0 8 * * 1'. First day of each month at noon: '0 12 1 * *'. Every weekday at 6 PM: '0 18 * * 1-5'. Every 15 minutes during business hours: '*/15 9-17 * * 1-5'. Twice daily at 8 AM and 8 PM: '0 8,20 * * *'. Quarterly on the first day at midnight: '0 0 1 1,4,7,10 *'. These cover 90 percent of scheduling needs. For unusual patterns, use a cron expression generator to build and verify the syntax.
Recommended Resources
AI pair programmer that helps you write code faster.
Cloud infrastructure for developers. Get $200 free credit.
Sponsored · We may earn a commission at no cost to you
Debugging and Best Practices
The most common cron mistakes are timezone mismatches (cron uses the system timezone, not UTC, unless configured otherwise), incorrect path variables (cron runs with a minimal environment, so use absolute paths for all commands), and permission issues. Always redirect output to a log file: '*/5 * * * * /path/to/script.sh >> /var/log/myjob.log 2>&1'. Test scripts manually before scheduling them. Use 'crontab -l' to list current jobs and 'crontab -e' to edit. Never use 'crontab -r' casually — it removes all cron jobs without confirmation. Set MAILTO to receive error notifications via email.
Cron Alternatives and Modern Scheduling
While cron remains the standard on Linux and macOS, modern alternatives offer additional features. Systemd timers on Linux provide better logging, dependency management, and can trigger on events beyond time. GitHub Actions cron syntax schedules CI/CD workflows. Cloud schedulers like AWS EventBridge, Google Cloud Scheduler, and Azure Logic Apps offer managed cron-like scheduling with monitoring and retry logic. For Kubernetes workloads, CronJobs are native resources. Regardless of the platform, understanding standard crontab syntax is fundamental because all of these tools use the same five-field format or close variations.
Related Free Tools
Related Articles
Frequently Asked Questions
How do I run a cron job every 5 minutes?
Use the expression '*/5 * * * *' which means every 5 minutes, every hour, every day. The */5 in the minute field is a step value meaning 'every 5th minute' — it triggers at minutes 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, and 55. For every 10 minutes, use '*/10 * * * *'. For every 30 minutes, use '*/30 * * * *' or equivalently '0,30 * * * *'. Always test with a simple echo command first to verify timing before running important scripts.
Why is my cron job not running?
The most common reasons cron jobs fail silently are: the script lacks execute permissions (run 'chmod +x script.sh'), the cron environment lacks your PATH variable (use absolute paths like '/usr/bin/python3' instead of just 'python3'), the script works interactively but fails in cron's minimal environment (source profile files or export variables explicitly), or there is a syntax error in the crontab entry. Add output logging with '>> /tmp/cronlog.txt 2>&1' to capture errors and check system logs with 'grep CRON /var/log/syslog'.
What is the difference between crontab -e and editing /etc/crontab?
The 'crontab -e' command edits the current user's personal crontab, stored in /var/spool/cron/. Jobs run as that user with that user's permissions. The file /etc/crontab is the system-wide crontab that includes an additional user field specifying which user should run each job. Use 'crontab -e' for user-specific tasks like backups or notifications. Use /etc/crontab for system-wide maintenance tasks that need to run as root or a service account. There is also /etc/cron.d/ for package-specific scheduled tasks.