Skip to main content
ubuntu-blog.com

Search Ubuntu Blog

Cron Syntax: How to Schedule Jobs on Ubuntu

Author: First published In Tutorials

A cron schedule is five fields before the command: minute (0-59), hour (0-23), day of month (1-31), month (1-12) and day of week (0-7, where 0 and 7 are Sunday). An asterisk means every value, a comma lists values, a hyphen is a range and a slash is a step. Run crontab -e on Ubuntu to add a line, and escape any percent sign in the command.

Covered in the guide: Chmod Calculator

Cron runs commands on a schedule, and a cron schedule is written as five fields in front of the command. The free cron builder turns a schedule you describe into that line and lists its next run times; this guide explains the syntax behind it, so you can read and write schedules by hand. It is part of our Ubuntu command-line series, next to the chmod guide.

What are the five cron fields?

Each crontab line starts with minute, hour, day of month, month and day of week, in that order, followed by the command. The crontab(5) manual lists the allowed values:

Field Allowed values
Minute 0-59
Hour 0-23
Day of month 1-31
Month 1-12, or names
Day of week 0-7 (0 or 7 is Sunday), or names

Four symbols build a field. An asterisk * stands for “first-last”. A hyphen makes an inclusive range, so 8-11 in the hour field means hours 8, 9, 10 and 11. A comma makes a list such as 1,15. A slash adds a step: 0-23/2 in the hour field is every other hour, and */2 means the same.

Names are allowed in the month and day-of-week fields. Use the first three letters, in any case, such as mon,wed,fri or jan-mar. Ranges and lists of names work too.

What do @daily, @hourly and @reboot mean?

They are special strings that replace the five fields. The crontab(5) manual lists them under its extensions:

String Same as Meaning
@reboot none Run once after reboot
@yearly or @annually 0 0 1 1 * Once a year
@monthly 0 0 1 * * Once a month
@weekly 0 0 * * 0 Once a week
@daily 0 0 * * * Once a day
@hourly 0 * * * * Once an hour

Write one of these, then the command, as in @daily /home/sam/bin/cleanup.sh. They are convenient, but they run at fixed times, such as midnight for @daily. Use five fields when you want a specific time.

What are some common cron schedules?

These are built from the field rules above. Each shows the five fields and what they mean.

Schedule Expression
Every 5 minutes */5 * * * *
Every hour, on the hour 0 * * * *
Every 2 hours 0 */2 * * *
Every day at 02:30 30 2 * * *
Twice a day, at 06:00 and 18:00 0 6,18 * * *
Weekdays at 09:00 0 9 * * 1-5
Sundays at 05:00 0 5 * * 0
First day of each month at midnight 0 0 1 * *

Two behaviors surprise people. The first is that if both the day-of-month and day-of-week fields are restricted, a job runs when either one matches. The manual’s example, 30 4 1,15 * 5, runs at 4:30 am on the 1st and 15th of each month and also every Friday. Leave one of the two as * when you want only one rule.

The second is that steps do not carry across fields. The manual says */23 in the hours field means hour 0 and hour 23 of each day, not every 23 hours. For an interval that does not divide evenly into the field, use a script or a test inside the command, as the manual’s NOTES section shows.

Why does the percent sign break a cron job?

Because cron treats an unescaped % in the command specially. The manual says the command runs up to the first newline or % character, and each unescaped percent sign “will be changed into newline characters, and all data after the first % will be sent to the command as standard input.”

So this line does not do what it looks like:

0 3 * * * tar czf /backup/home-$(date +%F).tgz /home/sam

Escape every percent sign with a backslash:

0 3 * * * tar czf /backup/home-$(date +\%F).tgz /home/sam

The other fix is to put the command in a script file and call the script from the crontab, where percent signs are ordinary characters.

How do I use crontab -e on Ubuntu?

Run crontab -e. The crontab(1) manual says it edits your current crontab with the editor named in the VISUAL or EDITOR environment variable, and that “the modified crontab will be installed automatically” when you exit the editor.

The other commands you will use:

  • crontab -l prints your current crontab.
  • crontab -T file tests a crontab file’s syntax without installing it, and stops at the first problem it finds.
  • crontab -r removes your crontab, so be careful: it asks no question unless you add -i.
  • sudo crontab -u name -e edits another user’s crontab.

Comments start with # at the beginning of a line. The manual warns that comments are not allowed on the same line as a command, because they would be treated as part of the command. Cron also needs every line to end with a newline, and treats a crontab whose last line lacks one as at least partly broken.

User crontab or system crontab?

Use your own crontab (crontab -e) for personal jobs. The files /etc/crontab and those in /etc/cron.d/ are system jobs, and they need an extra field. The manual says each line has “five time-and-date fields followed by a user name (if this is the system crontab file), and followed by a command.” For example:

MAILTO=root
0 4 * * * root /usr/local/bin/rotate-reports

Cron picks up changes to these files on its own; you do not restart it after editing.

How do I check that a cron job ran?

Redirect the job’s output to a file and read that first. Cron mails any output to the crontab owner, or to MAILTO if you set it, and an empty MAILTO="" turns mail off. The cron(8) manual says the -s option sends job output to syslog instead, which helps on a machine with no mail program.

For cron’s own messages, journalctl -u cron shows the log for a systemd unit named cron, using the -u option the journalctl manual describes. Some systems also write a /var/log/syslog file you can search for CRON.

Remember daylight saving changes. The crontab(5) manual warns that times skipped in the spring never match, and that repeated times in the autumn run a job twice, while cron(8) describes special handling for time changes of less than three hours for jobs that run at a specific time. If a job must run exactly once, avoid scheduling it in the hours around a clock change.

How do I build a cron expression with the tool?

Open the builder, choose a preset or type your own five fields, and read the plain-English explanation, the crontab line and the next run times. It warns about an unescaped percent sign and expands the @ strings into their five fields. Use it to check an expression before you paste it into crontab -e.

Worked example: a backup at 02:30 on weekdays

You want /home/sam/bin/backup.sh to run at 02:30 from Monday to Friday and keep its output.

  1. Minute: 30.
  2. Hour: 2.
  3. Day of month: any, so *.
  4. Month: any, so *.
  5. Day of week: Monday to Friday is 1-5.
  6. Add the command and send output to a log: >> /home/sam/backup.log 2>&1.

The line is:

30 2 * * 1-5 /home/sam/bin/backup.sh >> /home/sam/backup.log 2>&1

Make sure the script is executable first, with chmod u+x /home/sam/bin/backup.sh; the chmod guide explains why. For a one-off job or a timer-based alternative, see the alarm clock for Ubuntu, and for something worth scheduling weekly, the apt update routine.

Questions readers ask

What does */5 mean in cron?

It is a step value: the asterisk stands for first-last and the slash with 5 skips through that range in steps of five. In the minute field, it runs the job at minutes 0, 5, 10 and so on up to 55 of every hour. The crontab manual notes that steps are evaluated only within the field they are attached to, so a step of 23 in the hour field means hours 0 and 23, not every 23 hours.

How do I run a cron job at boot?

Use the special string @reboot in place of the five time fields, as in @reboot /home/sam/bin/start.sh. The crontab manual says @reboot means run once after reboot. The job runs when the cron daemon starts, which happens at boot, and runs once per boot, not repeatedly. Use absolute paths in the command: crontab(5) says cron runs it with /bin/sh unless the crontab sets SHELL.

Where do cron logs go on Ubuntu?

Cron mails a job's output to the crontab owner, or to the address in the MAILTO variable. The cron(8) manual says the -s option sends job output to syslog instead, which is useful when no mail program is installed. To see cron's own messages, run journalctl -u cron, or search your syslog file for CRON if your system writes one. Redirecting output to a file in the job itself is the clearest method.

What is the difference between a user crontab and the system crontab?

A user crontab is edited with crontab -e and runs as that user, with five time fields and then the command. The system files /etc/crontab and the files in /etc/cron.d/ add a user name field between the time fields and the command, because they are shared by more than one user. Per the cron(8) manual, the crontab command creates user crontabs in /var/spool/cron/crontabs.

Why does the percent sign break my cron job?

In a crontab line, the shell command ends at the first unescaped percent sign. Cron turns each unescaped percent sign into a newline and sends everything after the first one to the command as standard input. A command such as date +%F therefore fails. Escape each percent sign with a backslash, as in date +\%F, or move the command into a script file.

How do I test a crontab before I install it?

Run crontab -T file to check the syntax of a crontab file without installing it. The crontab manual says validation stops at the first problem it finds, so it does not list every issue at once. After installing with crontab -e, list the result with crontab -l, and for a new schedule set it to run in a couple of minutes first, to confirm that the command works under cron.

Further reading

  1. crontab(5) manual page, Ubuntu 24.04 (Noble) (manpages.ubuntu.com)
  2. crontab(1) manual page, Ubuntu 24.04 (Noble) (manpages.ubuntu.com)
  3. cron(8) manual page, Ubuntu 24.04 (Noble) (manpages.ubuntu.com)
  4. journalctl(1) manual page, Ubuntu 24.04 (Noble) (manpages.ubuntu.com)

Worth reading next

Top of page