Writing a cron expression is simple once the schedule is translated into the right fields. This guide explains standard cron syntax, provides reusable examples for Linux and CI jobs, and shows what to check when a recurring task runs at the wrong time—or does not run at all.
Overview
A cron expression is a compact description of when a command or workflow should run. In traditional Linux cron, the expression usually contains five time fields followed by the command:
* * * * * command-to-run
│ │ │ │ │
│ │ │ │ └─ day of week (0–7, usually Sunday)
│ │ │ └─── month (1–12)
│ │ └───── day of month (1–31)
│ └─────── hour (0–23)
└───────── minute (0–59)
The fields are evaluated from left to right: minute, hour, day of month, month, and day of week. An asterisk means “every allowed value.” Commas list specific values, hyphens define ranges, and slashes define steps.
| Expression | Meaning |
|---|---|
* | Every value in the field |
1,15 | At values 1 and 15 |
1-5 | Every value from 1 through 5 |
*/10 | Every 10 units, such as every 10 minutes |
0 9 * * 1-5 | At 09:00 on weekdays |
Before adding an entry to crontab, describe the schedule in plain English. For example, “at 02:30 every Sunday” becomes 30 2 * * 0. A cron builder or schedule tester can help validate the result, but the platform running the job remains the source of truth for supported syntax and time-zone behavior.
Checklist by scenario
Every weekday
To run a job at 08:30 from Monday through Friday, use:
30 8 * * 1-5 /path/to/script.sh
Some systems accept weekday names such as MON-FRI, but numeric values are more portable across standard cron implementations. Confirm whether Sunday is represented by 0, 7, or both if you use numeric weekday values.
Several times each day
To run at 09:00, 13:00, and 17:00 every day:
0 9,13,17 * * * /path/to/script.sh
Use a comma when the schedule contains a short list of exact times. This is clearer than creating three separate entries and keeps related schedules together.
Regular intervals
To run every 15 minutes:
*/15 * * * * /path/to/script.sh
To run every two hours at the top of the hour:
0 */2 * * * /path/to/script.sh
Step syntax does not always express “every interval from the moment the job was created.” It describes matching clock values. For example, */15 normally matches minute 0, 15, 30, and 45 of each hour.
Weekly maintenance
For a cleanup job at 03:00 every Sunday:
0 3 * * 0 /path/to/cleanup.sh
Use an absolute path to the script, and make sure the script has executable permissions. Cron often runs with a smaller environment than an interactive shell, so commands that work in a terminal may need explicit paths, environment variables, or a shell declaration.
Month-end work
“The last day of every month” is not represented consistently by standard five-field cron. A common-looking expression such as 0 0 31 * * runs only in months that have a 31st day; it does not mean the final calendar day.
For reliable month-end processing, use one of these approaches:
- Run daily near midnight and have the script check whether tomorrow is in a different month.
- Use a scheduler that explicitly supports a last-day modifier.
- Schedule a broader date range and make the job idempotent so it safely determines whether work is due.
CI jobs and cloud functions
Hosted systems often use cron-like syntax but add their own rules. GitHub Actions scheduled workflows use a five-field POSIX-style expression, while the workflow file also determines the branch and event context. A schedule such as:
on:
schedule:
- cron: '30 6 * * 1-5'
requests a weekday schedule, but the job still depends on repository configuration, permissions, and the workflow being available in the expected branch. Managed cloud schedulers may use five or six fields, require a seconds field, or let you select a time zone separately. Never copy an expression between Linux cron, Quartz, GitHub Actions, and a cloud scheduler without checking that platform’s field count and special characters.
What to double-check
- Field order: Standard cron starts with minute, not second. Quartz commonly includes a seconds field and may require a question mark in one date field.
- Time zone: Identify whether the scheduler uses the host machine’s local zone, UTC, a repository setting, or a per-job time-zone option. Write the intended zone beside the schedule in documentation.
- Daylight-saving changes: A local-time job can encounter a skipped hour in spring or a repeated hour in autumn. Decide whether duplicate execution is safe and whether missed work should be recovered.
- Missed runs: Traditional cron generally does not queue every run that occurs while a machine is stopped. CI and managed schedulers may have different behavior, including delayed, skipped, or overlapping runs.
- Overlap: If a task can run longer than its interval, use a lock, lease, or application-level idempotency check where supported.
- Output and errors: Redirect output to a known log destination or rely on the platform’s job logs. A silent failure is difficult to distinguish from a job that never started.
- Credentials: Supply secrets through the platform’s approved environment or secret mechanism rather than embedding them in the expression or command line.
Common mistakes
Confusing “every” with “at intervals.” 0 * * * * means once at the start of every hour. It does not mean every 60 minutes from the time you saved the job.
Using the wrong weekday convention. Sunday may be 0 or 7 in standard cron, but support varies. Test a date near the boundary of the week when the distinction matters.
Expecting month fields to calculate month-end dates. Cron matches fields; it does not automatically understand the final calendar day. Put date logic in the script or choose a scheduler with calendar-aware features.
Forgetting the execution environment. Cron may not load your shell profile, use the same working directory, or expose the same PATH as an interactive session. Use absolute paths and log the effective environment when troubleshooting.
Assuming a successful trigger means successful work. A scheduler can start a process that then fails. Monitor the command or workflow result, not just the existence of a schedule.
When to revisit
Review a cron expression before seasonal planning cycles, after changing servers or deployment platforms, and whenever a workflow moves from local cron to CI or a managed cloud scheduler. Recheck it when the application’s time zone, reporting cutoff, backup policy, or business calendar changes.
Keep a small schedule record with the plain-English intention, expression, time zone, owner, expected runtime, and recovery behavior. Test the command manually, then test the scheduler with a harmless output before connecting it to production data. For deployments, scheduled reports, backups, and cleanup tasks, confirm whether rerunning is safe and what happens after an outage.
When syntax is uncertain, use a cron expression builder to generate a candidate, then compare its interpretation with the target platform’s documentation. Treat the builder as a validation aid rather than a substitute for checking field count, time zone, missed-run handling, and overlap behavior. A readable explanation beside the expression will make the next change safer than relying on five symbols alone.