What Is a Cron Job?
A cron job is a time-based task scheduler built into Unix-like operating systems (Linux, macOS). It lets you automate repetitive tasks — running backups, clearing caches, sending reports, or deploying code — at precise intervals without human intervention.
The cron daemon runs in the background and checks the crontab (cron table) file every minute. When the current time matches a scheduled job, it executes the associated command. This makes cron one of the most reliable and widely-used scheduling tools in the developer ecosystem.
Crontab Syntax Explained
A cron expression consists of five fields separated by spaces. Each field represents a unit of time and accepts specific values or special characters.
| Field | Values | Description |
|---|---|---|
| Minute | 0–59 | Minute of the hour |
| Hour | 0–23 | Hour of the day (24-hour format) |
| Day | 1–31 | Day of the month |
| Month | 1–12 | Month of the year |
| Weekday | 0–6 | Day of week (0 = Sunday) |
Special Characters in Cron
Matches every value in the field. * * * * * runs every minute.
Specifies multiple values. 0 9,18 * * * runs at 9 AM and 6 PM.
Defines a range of values. 0 9 * * 1-5 runs at 9 AM Monday through Friday.
Defines intervals. */15 * * * * runs every 15 minutes. */2 in the hour field means every 2 hours.
Common Cron Job Examples
* * * * * 0 0 * * * 0 9 * * 1-5 */15 * * * * 0 0 1 * * 30 2 * * 0 Cron Job Best Practices
- 1
Log everything. Redirect output to a log file:
0 9 * * * /path/script.sh >> /var/log/myjob.log 2>&1 - 2
Use absolute paths. Cron runs with a minimal environment. Always use full paths for commands and files to avoid "command not found" errors.
- 3
Avoid overlapping jobs. If a job takes longer than its interval, use a lock file (
flock) to prevent multiple instances from running simultaneously. - 4
Test with short intervals first. Before setting a weekly job, test it with
* * * * *(every minute) to confirm the command works in the cron environment. - 5
Mind the timezone. Cron uses the system timezone by default. On servers, this is often UTC. Set the
TZvariable in your crontab if needed.
Frequently Asked Questions
How do I edit the crontab on Linux or macOS?
Run crontab -e to open the editor, add your schedule line, and save. Use crontab -l to list current jobs.
What does */5 mean in a cron expression?
The step syntax */5 means "every 5 intervals." In the minute field, it triggers at minutes 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, and 55.
Can I schedule a cron job for a specific date?
Yes, combine the day and month fields. For example, 0 0 25 12 * runs at midnight on December 25th every year. For one-time tasks, consider using at instead.
What timezone does cron use?
Cron uses the system's default timezone, which is often UTC on servers. You can override it by adding TZ=America/New_York at the top of your crontab file.