Backing up a database every night, clearing a cache every hour, generating a report every Monday morning: on a server these all run from cron, and a cron expression tells it when.
What is Cron?
Cron is a time-based job scheduler daemon found in Unix-like operating systems. Originally developed at AT&T Bell Labs in 1975, it remains the standard scheduler on Linux and macOS to this day. The name comes from "Chronos," the Greek word for time.
A cron expression is the syntax you use to tell the cron daemon exactly when to run your task.
Understanding the 5-Field Structure
A standard cron expression consists of five space-separated fields. Each one means this:
Minute (0-59): Specifies which minute the task runs. 0 means on the hour, 30 means at half past.
Hour (0-23): Uses 24-hour format. 0 is midnight, 13 is 1 PM.
Day of Month (1-31): Which day of the month to run. 1 means the first of every month.
Month (1-12): Which month to run. 1 is January, 12 is December.
Day of Week (0-7): Both 0 and 7 represent Sunday. 1 is Monday, 6 is Saturday.
For example, 30 9 * * 1 means "every Monday at 9:30 AM."
Special Characters Explained
Four special characters cover almost every schedule.
Asterisk (*): Means "every possible value." An asterisk in the minute field means every minute; in the hour field, every hour.
Comma (,): Lists multiple values. 1,3,5 means "1 and 3 and 5." In the day-of-week field, 1,3,5 means Monday, Wednesday, and Friday.
Hyphen (-): Defines a range. 1-5 means "1 through 5." In the day-of-week field, that translates to Monday through Friday.
Slash (/): Specifies intervals. */5 means "every 5th." In the minute field, */10 runs every 10 minutes.
Commonly Used Cron Expressions
Patterns you can copy directly.
Every minute: * * * * * - Useful for testing or real-time monitoring.
Every hour on the hour: 0 * * * * - Great for log collection or health checks.
Every day at midnight: 0 0 * * * - The classic choice for daily backups and log rotation.
Every day at 6 AM: 0 6 * * * - Perfect for morning report delivery.
Every Monday at 9 AM: 0 9 * * 1 - Ideal for weekly report generation.
First of every month at midnight: 0 0 1 * * - Used for monthly billing or statistics aggregation.
Weekdays at 8 AM: 0 8 * * 1-5 - When you need tasks to run only on business days.
Every 5 minutes: */5 * * * * - Common for API polling or health checks.
Every 30 minutes: */30 * * * * - Well-suited for data synchronization tasks.
Working with crontab Commands
On Linux systems, you manage cron jobs using the crontab command.
View current jobs: crontab -l
Edit jobs: crontab -e - Opens your default editor where you write the expression followed by the command on a single line.
Remove all jobs: crontab -r - Use with caution. It deletes everything without confirmation.
When adding a job, write something like 0 2 * * * /home/user/backup.sh. Always use absolute paths since cron jobs run with a minimal environment and may not load your shell profile.
Real-World Use Cases
Log file cleanup: Server logs accumulate and eat disk space over time. Scheduling a job at 3 AM daily to remove logs older than 7 days keeps things tidy. 0 3 * * * find /var/log -name "*.log" -mtime +7 -delete
Database backups: Data loss can be catastrophic. A common approach is to create a database dump at 2 AM daily and transfer it to a backup server.
Cache warming: Services using Redis or Memcached benefit from periodically pre-loading popular data into cache. Running a cache warm-up script every hour keeps response times fast.
SSL certificate renewal: If you use Let's Encrypt, automatic renewal before expiration is essential. Attempting renewal on the 1st and 15th of each month provides a safety buffer.
Tips and Common Pitfalls
Check server timezone: Cron uses the system's timezone setting. If your server runs on UTC but you need tasks on local time, account for the offset. This is one of the most common mistakes people make.
Prevent overlapping runs: If a task takes longer than expected, the next scheduled run might start while the previous one is still going. Use a locking mechanism like flock to prevent duplicate executions.
Always log output: Cron jobs run silently in the background. When something breaks, you might not notice for days. Redirect output to a log file or configure email notifications.
Test with short intervals: When setting up a new cron job, start with a per-minute schedule to verify it works correctly, then change to your desired interval once confirmed.
To build and check expressions interactively, use the tool below.