DevOps & Automation

Mastering Unix Cron Expressions: Complete Syntax, Special Characters & Edge Cases

By Sohil Mohamed • 1,250 words • 10 min read

Automated background scheduling is the backbone of production systems. Whether you are running midnight database backups, synchronizing third-party API caches, rotating application log files, or triggering transactional email digests, the Unix cron daemon remains the standard time-based job scheduler. However, despite being a 50-year-old utility, cron expressions remain notorious for subtle syntax traps that can trigger silent failures or duplicate executions in production.

Author Note: Cron expressions can look confusing when you first encounter them, especially when you need a task to run at a specific time or repeat on certain days. I find it easier to start with the schedule I want in plain English and then translate it into the expression. Testing the result is important because different schedulers may interpret expressions differently.

1. The Standard 5-Field Cron Structure

In standard Unix and Linux systems (such as Vixie cron, cronie, and standard systemd timers), a crontab schedule entry consists of exactly five space-separated fields followed by the command to execute:

┌───────────── Minute (0 - 59) │ ┌───────────── Hour (0 - 23) │ │ ┌───────────── Day of the Month (1 - 31) │ │ │ ┌───────────── Month of the Year (1 - 12 or JAN - DEC) │ │ │ │ ┌───────────── Day of the Week (0 - 6 or SUN - SAT; 0 and 7 are both Sunday) │ │ │ │ │ * * * * * /usr/local/bin/backup.sh

2. The 5 Core Special Character Operators

Understanding how cron operators interact is critical for constructing robust automation rules:

3. The Infamous "Day of Month" vs "Day of Week" Trap

The single most common bug in production cron configurations involves specifying both the Day of Month (Field 3) and the Day of Week (Field 5).

In standard Unix crontab specification, fields are evaluated using a logical AND relationship — except when both Day of Month and Day of Week are explicitly defined! When both fields are set to values other than an asterisk, cron treats them with a logical OR condition.

Consider this schedule: 0 2 15 * 5
Many developers believe this means: "Run at 2:00 AM on the 15th of the month ONLY IF it is a Friday."
What cron actually does: It runs at 2:00 AM on the 15th of every month PLUS every Friday of the month!

If you truly require a task to run only on the 15th when it falls on a Friday, you must execute the condition within the shell command itself:

0 2 15 * * [ $(date +\%u) -eq 5 ] && /usr/local/bin/task.sh

4. The 6-Field Extended Cron Format (Spring, Quartz, CloudWatch)

While standard Unix systems use 5 fields, modern enterprise frameworks like Quartz Scheduler, Spring Boot, AWS CloudWatch Events, and Kubernetes CronJobs frequently employ a 6-field format.

In Java / Spring schedulers, the leading field represents Seconds (0-59):

0 0 12 * * ? (Runs every day at 12:00:00 PM)

In these extended engines, the question mark (?) is used to satisfy the syntax requirement for "no specific value" when resolving the Day of Month / Day of Week conflict.

5. Environment Variables & Path Pitfalls in Crontab

A frequent frustration for software engineers is a bash script that executes flawlessly when run manually in terminal, but fails completely when invoked by cron. Why does this happen?

Cron runs commands in an extremely sparse, non-interactive shell environment. It does not load your interactive shell profile (such as ~/.bashrc, ~/.zshrc, or /etc/profile). Consequently:

6. Real-World Production Recipe Patterns

Schedule Objective Cron Expression Explanation
Every 5 Minutes */5 * * * * Fires at :00, :05, :10, :15, etc.
Daily Midnight Cleanup 0 0 * * * Executes at 00:00 every calendar day.
Off-Peak Database Backup 30 2 * * * Executes at 2:30 AM when server load is lowest.
Weekday Business Hours 0 9-17 * * 1-5 At the start of every hour from 9 AM to 5 PM, Mon-Fri.
Weekly Sunday Audit 0 4 * * 0 Fires every Sunday at 4:00 AM.
First Day of Month 0 0 1 * * Runs at midnight on the 1st of every month.

7. Best Practices for Production Automation

8. Build & Test Your Schedules Online

To eliminate guesswork and generate bulletproof cron schedules visually with real-time plain-English translation, use our free interactive utility:

Launch Black Claaw Free Cron Expression Generator →