Cron Jobs Explained: A Beginner's Guide to Task Scheduling
Cron is one of the most useful utilities in Unix-like systems. It lets you schedule tasks (called "cron jobs") to run automatically at specific times — whether that's backing up a database every night, sending a weekly report, or clearing temporary files every hour. This guide will take you from zero to confidently writing cron expressions.
The Five-Field Format
A cron expression consists of five fields separated by spaces, each representing a unit of time:
┌───────────── minute (0-59) │ ┌───────────── hour (0-23) │ │ ┌───────────── day of month (1-31) │ │ │ ┌───────────── month (1-12) │ │ │ │ ┌───────────── day of week (0-7, 0 and 7 = Sunday) │ │ │ │ │ * * * * *
Each field can contain:
- A specific number —
5means "at the 5th unit" - An asterisk (*) — Means "every" (wildcard)
- A range —
1-5means Monday through Friday - A list —
1,15means the 1st and 15th - A step —
*/5means "every 5 units"
Common Cron Schedules
| Expression | Schedule |
|---|---|
* * * * * | Every minute |
*/5 * * * * | Every 5 minutes |
0 * * * * | Every hour (at minute 0) |
0 0 * * * | Every day at midnight |
0 9 * * * | Every day at 9:00 AM |
0 9 * * 1 | Every Monday at 9:00 AM |
0 0 1 * * | First day of every month at midnight |
0 0 * * 0 | Every Sunday at midnight |
30 2 * * * | Every day at 2:30 AM |
0 9-17 * * 1-5 | Every hour from 9 AM to 5 PM, weekdays only |
0 0 1,15 * * | 1st and 15th of every month at midnight |
Special Strings
Many cron implementations support shorthand strings:
@yearly(or@annually) —0 0 1 1 *— Once a year, January 1st@monthly—0 0 1 * *— First day of every month@weekly—0 0 * * 0— Every Sunday@daily(or@midnight) —0 0 * * *— Every day at midnight@hourly—0 * * * *— Every hour@reboot— Run once at system startup
Working with Crontab
On Linux/macOS, you manage cron jobs through the crontab (cron table) file:
# View your current cron jobs crontab -l # Edit your cron jobs crontab -e # Example: Run a backup script every day at 2 AM 0 2 * * * /home/user/scripts/backup.sh # Example: Clean temp files every hour 0 * * * * find /tmp -mtime +7 -delete # Example: Send a weekly report every Friday at 5 PM 0 17 * * 5 /home/user/scripts/weekly-report.sh
Best Practices
- Use absolute paths — Cron runs with a minimal environment. Always use full paths for scripts and commands.
- Redirect output — By default, cron emails output to the user. Redirect to a log file:
0 2 * * * /path/script.sh >> /var/log/myjob.log 2>&1 - Set the PATH — Add
PATH=/usr/local/bin:/usr/bin:/binat the top of your crontab. - Use lock files — Prevent overlapping runs if a job takes longer than the interval.
- Test first — Run your command manually before scheduling it. A cron job that fails silently wastes time.
- Document your jobs — Add comments above each entry explaining what it does and why.
Build Cron Expressions Visually
No need to memorize syntax. Our visual builder lets you click to create cron expressions with instant descriptions and next run times.
Try Cron Expression GeneratorCommon Pitfalls
- Timezone confusion — Cron uses the system timezone. If your server is in UTC but you expect local time, adjust accordingly.
- Day-of-month vs. day-of-week — When both are specified, cron runs if either matches (OR logic), not both (AND logic). This catches many people off guard.
- February 30th —
0 0 30 2 *will never run because February never has 30 days. No error is raised. - Environment differences — Your cron job runs in a different environment than your terminal. Variables, paths, and shell settings may differ.
- Email storms — A job running every minute that produces output will generate 1,440 emails per day. Always redirect output.
Alternatives to Cron
While cron is the classic tool, modern systems offer alternatives:
- systemd timers — More flexible than cron on modern Linux systems, with better logging and dependency management.
- Cloud schedulers — AWS CloudWatch Events, Google Cloud Scheduler, and Azure Functions Timers for serverless scheduled tasks.
- CI/CD pipelines — GitHub Actions and GitLab CI support scheduled workflows using cron syntax.
- Task queues — Celery (Python), Sidekiq (Ruby), and Bull (Node.js) for more complex scheduling needs.
Cron remains the most straightforward way to schedule recurring tasks. Use our Cron Expression Generator to build expressions visually and verify them before deploying to production.