1. The Classic 5-Field Cron Expression
Standard UNIX cron schedule expressions consist of exactly five fields separated by white space:
2. Special Operators & Modifiers
You can combine four primary operators inside any cron field to define intricate recurrence rules:
- Asterisk (
*): Wildcard meaning "every possible value" for this field. - Comma (
,): Value list (e.g.1,15,30in the minute field runs at minutes 1, 15, and 30). - Hyphen (
-): Range of values (e.g.1-5in day-of-week runs Monday through Friday). - Slash (
/): Step values (e.g.*/15in minute runs every 15 minutes: 0, 15, 30, 45).
3. Cheatsheet of Common Production Cron Schedules
| Schedule | Cron Expression | Human Meaning |
|---|---|---|
| Every 5 minutes | */5 * * * * | Queue worker healthcheck or cache warm |
| Every hour at minute 0 | 0 * * * * | Hourly metrics aggregation |
| Every night at 2:30 AM | 30 2 * * * | Daily database backup snapshot |
| Every Monday at 9:00 AM | 0 9 * * 1 | Weekly billing and email digest |
| First of every month at midnight | 0 0 1 * * | Monthly customer invoicing cycle |
4. The #1 Automation Trap: Stripped Environment Variables
The single most common bug when deploying cron scripts: "My script runs when I type it in bash, but fails when executed by cron!"
When the cron daemon runs a task, it does not load your interactive shell profile (such as .bashrc or .zshrc). Its $PATH variable is severely stripped down (often to only /usr/bin:/bin).
Therefore, commands like node, python3, or docker cannot be found unless you supply the absolute path:
# ❌ Fails: cron doesn't know where python3 or backup.py is located 0 2 * * * python3 backup.py # ✅ Works: Always specify absolute binary and file paths 0 2 * * * /usr/bin/python3 /var/www/scripts/backup.py
5. Output Redirection & Logging
By default, cron attempts to send output through local mail (sendmail / postfix), which is rarely configured on modern cloud servers. If your task errors, the error output is lost silently.
Always redirect both standard output (stdout) and standard error (stderr) to a log file:
# Redirect stdout (1) and stderr (2) to the same logfile 0 2 * * * /usr/bin/node /app/clean.js >> /var/log/clean.log 2>&1