2026-02-25•3 min read•by DevUtilz
Cron Expression Tutorial
CronSchedulingDevOpsTutorial
Cron Expression Tutorial
Cron is a time-based job scheduler for Unix-like systems. Master cron expressions to automate tasks effectively.
Cron Format
A cron expression has 5 fields:
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6)
│ │ │ │ │
* * * * *
Special Characters
| Character | Meaning | Example |
|-----------|---------|---------|
| * | Any value | * * * * * = every minute |
| , | List separator | 1,15 * * * * = at 1 and 15 min |
| - | Range | 0 9-17 * * * = every hour 9-5 |
| / | Step | */15 * * * * = every 15 minutes |
Common Examples
Every Minute
* * * * *
Every Hour
0 * * * *
Every Day at Midnight
0 0 * * *
Every Monday at 9 AM
0 9 * * 1
First Day of Month
0 0 1 * *
Every 15 Minutes
*/15 * * * *
Weekdays 9-5
0 9-17 * * 1-5
Practical Examples
Database Backup (Daily at 2 AM)
0 2 * * * /path/to/backup.sh
Send Weekly Report (Sunday at 6 PM)
0 18 * * 0 /path/to/send-report.sh
Clear Cache (Every Hour)
0 * * * * rm -rf /tmp/cache/*
Sync Data (Every 5 Minutes)
*/5 * * * * /path/to/sync.sh
Monthly Cleanup (1st of month at 3 AM)
0 3 1 * * /path/to/cleanup.sh
Node.js with Cron
Using node-cron:
const cron = require('node-cron');
const task = cron.schedule('0 2 * * *', () => {
console.log('Running daily backup');
runBackup();
});
// Schedule multiple tasks
cron.schedule('*/15 * * * *', () => {
syncData();
});
cron.schedule('0 9 * * 1', () => {
sendWeeklyReport();
});
Cron in Cloud Platforms
Vercel Cron Jobs (vercel.json)
{
"crons": [
{
"path": "/api/backup",
"schedule": "0 2 * * *"
}
]
}
GitHub Actions
name: Daily Backup
on:
schedule:
- cron: '0 2 * * *'
AWS CloudWatch Events
{
"scheduleExpression": "cron(0 2 * * ? *)"
}
Debugging Cron Issues
Common Problems
- Script not running - Check permissions
- Wrong timezone - Use timezone in expression
- Environment variables - Use absolute paths
Testing Your Cron
# List scheduled jobs
crontab -l
# Check system cron
ls /etc/cron.d/
# View logs
grep CRON /var/log/syslog
Advanced: Timezones
// Node-cron with timezone
cron.schedule('0 9 * * *', () => {
console.log('Runs at 9 AM EST');
}, {
scheduled: true,
timezone: 'America/New_York'
});
Human-Readable Alternatives
Instead of memorizing expressions, use:
const cronstrue = require('cronstrue');
console.log(cronstrue.toString('0 9 * * 1'));
// Output: "At 09:00 on Monday"
Conclusion
Cron expressions are powerful for scheduling. Start with simple patterns and build up. Always test your expressions before deploying to production.