DEV Community

arenasbob2024-cell
arenasbob2024-cell

Posted on • Originally published at viadreams.cc

Cron Expressions: Complete Guide for DevOps and Backend Developers

Cron expressions are the universal language for scheduling tasks. Whether you're scheduling database backups, sending reports, or deploying code, you need cron. Here's your complete reference.

The 5-Field Format

┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sun=0)
* * * * *
Enter fullscreen mode Exit fullscreen mode

Patterns You'll Use Daily

*/5 * * * *     Every 5 minutes
0 * * * *       Every hour (on the hour)
0 0 * * *       Daily at midnight
0 9 * * 1-5     Weekdays at 9 AM
0 0 1 * *       First of every month
0 0 * * 0       Every Sunday
30 2 * * *      Daily at 2:30 AM
0 */4 * * *     Every 4 hours
0 9,17 * * *    At 9 AM and 5 PM
Enter fullscreen mode Exit fullscreen mode

Special Characters

Character Meaning Example
* Any value * * * * * (every minute)
, List 1,15 * * * * (minute 1 and 15)
- Range 0 9-17 * * * (9 AM to 5 PM)
/ Step */10 * * * * (every 10 minutes)
L Last 0 0 L * * (last day of month)
W Weekday 0 0 15W * * (nearest weekday to 15th)
# Nth weekday 0 0 * * 5#2 (2nd Friday)

Platform Differences

Linux crontab

# Edit crontab
crontab -e

# List jobs
crontab -l

# Example: backup daily at 2 AM
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
Enter fullscreen mode Exit fullscreen mode

GitHub Actions

on:
  schedule:
    - cron: '0 9 * * 1-5'  # UTC timezone only!
Enter fullscreen mode Exit fullscreen mode

Kubernetes CronJob

apiVersion: batch/v1
kind: CronJob
metadata:
  name: daily-report
spec:
  schedule: "0 9 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: report
            image: report-generator:latest
          restartPolicy: OnFailure
Enter fullscreen mode Exit fullscreen mode

AWS EventBridge

cron(0 9 ? * MON-FRI *)
Enter fullscreen mode Exit fullscreen mode

Note: AWS uses ? for "no specific value" and 6 fields (including year).

Common Mistakes

  1. Timezone confusion — Most cron systems use UTC. Always specify timezone:
CRON_TZ=America/New_York
0 9 * * * /path/to/script.sh
Enter fullscreen mode Exit fullscreen mode
  1. Day-of-week numbering — Standard: 0=Sun, 6=Sat. Some systems: 1=Mon, 7=Sun.

  2. Overlapping runs — If job takes 10 minutes but runs every 5:

# Use flock to prevent overlapping
*/5 * * * * flock -n /tmp/myjob.lock /path/to/script.sh
Enter fullscreen mode Exit fullscreen mode
  1. Missing PATH — Cron has minimal environment:
PATH=/usr/local/bin:/usr/bin:/bin
0 * * * * node /app/script.js
Enter fullscreen mode Exit fullscreen mode

Debugging Cron Jobs

# Check cron logs
grep CRON /var/log/syslog

# Redirect output to a log file
0 * * * * /path/to/script.sh >> /var/log/myjob.log 2>&1

# Send output via email (if mail is configured)
MAILTO=admin@example.com
0 0 * * * /path/to/daily-report.sh
Enter fullscreen mode Exit fullscreen mode

Try It Online

Parse, validate, and understand cron expressions with DevToolBox's Cron Parser — human-readable descriptions, next 10 run times, visual schedule.


What's the most complex cron expression in your infrastructure? Share below!

Top comments (0)