Skip to content
Kaspar

Crontab Syntax Explained: How to Read and Write Cron Expressions

Crontab syntax explained: the five fields, special characters like */5, common examples, and the pitfalls around weekdays, percent signs, PATH and time zones.

Kaspar 11 min read

Five fields, a few asterisks and a command: at first glance, crontab syntax looks simple. Yet a misread cron expression is one of the most common reasons a job runs at the wrong time or not at all. The backup is set for 2:30 a.m., but in the wrong time zone. The month-end report suddenly runs every Friday. A command breaks on a single percent sign.

In this article we walk through crontab syntax field by field, list the expressions you will use most often, and explain the pitfalls we see again and again in practice. All examples refer to the classic Linux cron daemon that ships with Debian and Ubuntu. Where other implementations behave differently, we point it out.

The anatomy of a crontab line

Every active line in your crontab consists of five time fields followed by the command to run. Fields are separated by spaces or tabs:

┌───────────── minute (0–59)
│ ┌─────────── hour (0–23)
│ │ ┌───────── day of month (1–31)
│ │ │ ┌─────── month (1–12 or jan–dec)
│ │ │ │ ┌───── day of week (0–7, 0 and 7 = Sunday, or sun–sat)
│ │ │ │ │
30 2 * * * /usr/local/bin/backup.sh

This line runs backup.sh every day at 2:30 a.m. Once a minute, cron checks whether the minute, hour and month match the current time, and whether at least one of the two day fields does. That "at least one" is where one of the biggest traps hides, and we will get to it shortly.

You edit your personal crontab with crontab -e and list it with crontab -l. Commands run with your user's permissions. The system-wide /etc/crontab and the files in /etc/cron.d/ have an extra sixth field for the user name, placed between the schedule and the command:

30 2 * * * root /usr/local/bin/backup.sh

If you copy a line from /etc/crontab into your personal crontab and keep the user field, cron will try to run a command called root. The reverse mistake is just as common: in /etc/cron.d/, a missing user field means the first word of your command is read as the user name.

Special characters: asterisk, comma, hyphen and slash

Four characters cover almost any schedule:

Character Meaning Example in the hour field
* every value * = every hour
, list 8,12,18 = at 8 a.m., noon and 6 p.m.
- range (inclusive) 9-17 = from 9 a.m. through 5 p.m.
/ step */2 = every other hour, 8-18/2 = 8, 10, …, 18

You can combine them, for example 0-4,8-12 for two ranges in one field. For months and weekdays you can also use the first three letters of the English name, such as jan or mon. The documentation for Debian's cron, however, says ranges and lists of names like mon-fri are not supported, and not every implementation understands them. 1-5 works everywhere, which is why we recommend sticking to numbers.

Steps restart in every period

One detail people often miss: */7 in the minute field does not mean "every seven minutes." It means "at minutes 0, 7, 14, …, 56 of every hour." At the top of the next hour, the count starts over. So between 8:56 and 9:00 there are only four minutes. You only get even spacing with steps that divide the range cleanly: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20 or 30 for minutes, and 1, 2, 3, 4, 6, 8 or 12 for hours.

Common cron expressions

These expressions cover most of the jobs we see in the wild:

Cron expression Meaning
* * * * * every minute
*/5 * * * * every five minutes
0 * * * * at the top of every hour
30 2 * * * every day at 2:30 a.m.
0 22 * * 1-5 Monday through Friday at 10 p.m.
*/15 9-17 * * 1-5 every 15 minutes from 9:00 a.m. to 5:45 p.m. on weekdays
0 3 * * 0 Sundays at 3 a.m.
0 0 1 * * at midnight on the first of every month
0 0 1 */3 * quarterly, on January 1, April 1, July 1 and October 1

Note the end of the 9-17 example: the last run is at 5:45 p.m., not 5:00 p.m., because the whole 17:00 hour is included.

Shortcuts like @daily and @reboot

Instead of five fields, most cron implementations also accept a few shortcuts:

Shortcut Equivalent
@hourly 0 * * * *
@daily or @midnight 0 0 * * *
@weekly 0 0 * * 0
@monthly 0 0 1 * *
@yearly or @annually 0 0 1 1 *
@reboot once, when the cron daemon starts

Be careful with @reboot. The job starts as soon as the cron service comes up, which can be before your database, network or other services are ready. For anything that should run continuously, a systemd unit is usually the more robust choice.

The day-of-week trap: day of month or day of week

This rule catches almost everyone at least once: if both the day-of-month and day-of-week fields are restricted, meaning neither is *, the job runs when either one matches. Not only when both match.

0 9 13 * 5 /usr/local/bin/report.sh

The intent was probably "every Friday the 13th at 9 a.m." In reality, the job runs on the 13th of every month and on every Friday. To require both conditions, restrict only one field in cron and check the other in the command:

0 9 13 * * [ "$(date +\%u)" = "5" ] && /usr/local/bin/report.sh

date +%u prints the day of the week as a number from 1 (Monday) to 7 (Sunday). We will explain the backslash before the percent sign below.

One quirk of Debian's cron: it only checks whether a day field starts with *. That means */2 in the day-of-month field counts as unrestricted. The expression 0 0 */2 * 0 therefore runs only on Sundays with an odd date there, while under POSIX it would run on every Sunday and every odd day. If you rely on combinations like this, test them on the exact system that will run the job.

What crontab syntax can't do

Some schedules simply can't be expressed with five fields:

  • Seconds. The smallest unit is one minute. If you need something to run more often, you need a long-running process or a different scheduler.
  • The last day of the month. Classic cron has no L like some other schedulers do. The usual workaround is to start the job on every candidate day and check in the command whether tomorrow is the first. This example uses GNU date, which is standard on Linux:
55 23 28-31 * * [ "$(date -d tomorrow +\%d)" = "01" ] && /usr/local/bin/month-end.sh
  • Intervals that don't fit evenly into an hour or a day. "Every 90 minutes" takes two alternating lines:
0 0-21/3 * * *   /usr/local/bin/sync.sh
30 1-22/3 * * *  /usr/local/bin/sync.sh

The first line runs at 0:00, 3:00, …, 21:00, the second at 1:30, 4:30, …, 22:30. Together they give you exactly 90 minutes between runs, including across midnight.

Pitfalls in the command part

A correct schedule doesn't help much if the command behaves differently under cron than in your terminal. These are the issues that cause the most trouble.

Escape percent signs

Cron turns any unescaped % in the command into a newline, and everything after the first % is passed to the command as standard input. So date +%F > /tmp/date.txt becomes just date +, and the redirect to the file never happens. The correct version:

0 1 * * * tar czf /backup/www-$(date +\%F).tar.gz /var/www

This applies to every command with date formats, printf or URL-encoded parameters.

Cron runs with a minimal environment

Cron doesn't read your .bashrc or .profile. On Debian and Ubuntu, a job starts with PATH=/usr/bin:/bin and /bin/sh as its shell. Programs in /usr/local/bin, version managers for PHP or Node, and environment variables from your shell are all missing. Use absolute paths, or set your own PATH at the top of the crontab:

PATH=/usr/local/bin:/usr/bin:/bin
SHELL=/bin/bash

Keep in mind that cron does not expand variables in these lines. PATH=$HOME/bin:$PATH won't work; you have to spell out the full path.

Comments, line endings and output

  • A # in the middle of a command line is not a comment. It is passed to the shell as part of the command. Put comments on their own lines.
  • Every line must end with a newline, including the last one. If it's missing, cron considers the crontab at least partially broken and logs a warning to syslog.
  • Cron mails any output to the crontab owner or to the address in MAILTO. If the server can't send mail, that output is lost. It's better to redirect output to a log file, for example with >> /var/log/backup.log 2>&1.

Prevent overlapping runs

Cron starts a job on schedule even if the previous run hasn't finished. If an import runs every five minutes and takes seven on a bad day, you suddenly have two instances running at once. flock from util-linux prevents that:

*/5 * * * * flock -n /tmp/import.lock /usr/local/bin/import.sh

If the lock is still held, flock -n exits immediately with exit code 1 instead of starting a second run.

Time zones and daylight saving time

Classic cron uses the time zone the daemon runs in, which in practice is the server's system time zone. Many cloud servers are set to UTC. On such a server, 0 6 * * * runs at 1 a.m. Eastern Time in winter and at 2 a.m. in summer. You can check the setting with timedatectl or date.

Debian's cron has no per-crontab time zone. A TZ variable in the crontab only changes the environment of the commands, not the time they start. Cronie, the implementation used in Fedora and RHEL, offers a CRON_TZ variable for this.

Daylight saving time is where it gets tricky. In the US, clocks jump from 2:00 to 3:00 a.m. in March, and in November the hour from 1:00 to 2:00 a.m. happens twice; in Europe, the switch happens on different dates and at different local times. Jobs scheduled inside the affected hour may run late, not at all, or twice on those days, depending on the implementation and its settings. The simplest fix is to keep important jobs out of the early-morning hours around the switch, or to run your servers in UTC throughout.

Other schedulers, other dialects

The five-field format has spread far beyond Linux, but not every scheduler reads it the same way:

  • Laravel Scheduler: You define tasks in PHP. The crontab has a single entry that calls php artisan schedule:run every minute. Expressions in ->cron('…') use the familiar five-field syntax.
  • Kubernetes CronJobs: also five fields. You set the time zone with .spec.timeZone; without it, the controller's time zone applies.
  • GitHub Actions: on: schedule uses five fields, always runs in UTC, and runs at most every five minutes.
  • Quartz and Spring: These add a seconds field at the start. In Spring, 0 30 2 * * * means every day at 2:30:00 a.m. In classic cron, the same expression would not even be valid.
  • AWS EventBridge: six fields, with a year field at the end and a ? in one of the two day fields.

So when you copy an expression from documentation, first check which scheduler it was written for.

Check cron expressions before they go live

Before a new expression goes into your crontab, it's worth a second look. With our free cron expression explainer you paste an expression, read what it means field by field, and see the next five run times in the time zone you choose. Mistakes in combined day fields or unusual steps show up there before they cause any harm.

Correct syntax, however, does not guarantee the job actually runs. A deployment can overwrite the crontab, the cron service can be missing after a reboot, or the script can fail in cron's minimal environment. That's what heartbeat monitoring is for: after every run, the job checks in with an external service, and if the check-in doesn't arrive, you get an alert. We explain how this works in detail in our guide on how to monitor cron jobs with heartbeats.

Conclusion

Crontab syntax is compact, but not always intuitive. Most mistakes happen in the same places: day of month and day of week are combined with "or," steps restart every hour, percent signs need escaping, and cron runs with a minimal environment in the server's time zone. If you know these rules, use absolute paths and check new expressions before you deploy them, you avoid most of the surprises.

In Kaspar, a cron monitor takes the exact expression from your crontab, together with the time zone your server runs the job in. Kaspar uses it to calculate when the next ping is due, accounts for daylight saving time, and alerts you by email, Slack, Telegram or Webhook when a ping is late, the job reports a failure or it exceeds its maximum runtime. If incoming pings match a different time zone than the one you configured, we point that out. Uptime checks and scans for broken links run in the same account. You can find an overview of all features and plans on our website.

  • Cron monitoring
  • Crontab
  • DevOps
  • Linux

Related articles