What is Cron?
Cron is the most common task scheduler on Unix/Linux systems, used to automatically execute commands or scripts at specified times. Cron expressions define the precise execution schedule.
Cron Expression Format
Standard Format (5 fields)
┌───── minute (0-59)
│ ┌───── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌───── month (1-12)
│ │ │ │ ┌───── day of week (0-7, 0&7=Sunday)
│ │ │ │ │
* * * * *
Special Characters
| Char | Meaning | Example |
|---|---|---|
* |
All values | * = every minute/hour |
, |
List separator | 1,3,5 = positions 1,3,5 |
- |
Range | 1-5 = 1 through 5 |
/ |
Step | */5 = every 5 |
Common Examples
| Expression | Meaning |
|---|---|
0 3 * * * |
Daily at 3:00 AM |
*/5 * * * * |
Every 5 minutes |
0 9-17 * * 1-5 |
Weekdays hourly 9-5 |
0 0 1 * * |
1st of month midnight |
0 0 * * 0 |
Weekly Sunday midnight |
30 4 * * 1-5 |
Weekdays 4:30 AM |
Best Practices
1. Avoid Peak Hours
Default cron tasks often trigger at midnight (0 0 * * *). Stagger execution times to avoid resource contention.
2. Use Validation Tools
Verify your cron expressions with tools like crontab.guru before deployment.
3. Distributed Systems
- Use distributed locks (e.g., Redis Lock) to prevent duplicate execution
- Consider distributed schedulers (e.g., Quartz Cluster)
4. Timezone Handling
Server cron uses system timezone (usually UTC). Account for timezone offsets in cross-border operations.
5. Logging
Always add logging to cron tasks for debugging and monitoring.