Skip to content
Log In Sign Up info@muvcloud.com

The Shape of a Line

# m  h  dom mon dow  command
  30 2  *   *   *    /usr/local/bin/backup.sh

This job runs every day at 02:30. The fields are separated by spaces:

FieldValues
Minute0-59
Hour0-23
Day of month1-31
Month1-12 or jan-dec
Day of week0-7 or sun-sat

In the day-of-week field, both 0 and 7 mean Sunday. Names use the first three letters and ignore case.

Ranges, Lists, and Steps

  • Star: * means every value of the field.
  • Range: 1-5 in the day-of-week field means Monday to Friday.
  • List: 1,15 in the day-of-month field means the 1st and the 15th.
  • Step: */15 in the minute field means every 15 minutes; 0-30/10 means minutes 0, 10, 20, and 30.

The cron of Debian and Ubuntu does not accept names inside ranges or lists (cronie on AlmaLinux and Rocky Linux does), so use numbers there.

When Both Day Fields Are Set

If both day of month and day of week are restricted, the job runs when either one matches. 0 9 1 * 1 runs on the 1st of every month and on every Monday, not only on Mondays that fall on the 1st. To require both, put the second test in the command. This runs on the first Monday of the month:

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

Special Strings

StringSame as
@rebootOnce at startup
@hourly0 * * * *
@daily0 0 * * *
@weekly0 0 * * 0
@monthly0 0 1 * *
@yearly0 0 1 1 *

@midnight is the same as @daily, and @annually the same as @yearly. A special string replaces all five time fields.

Examples

LineRuns
*/5 * * * *Every 5 minutes
0 * * * *Every hour
0 */6 * * *Every 6 hours
0 3 * * *03:00 daily
0 3 * * 003:00 on Sundays
30 8 * * 1-508:30 on weekdays
15 2 1,15 * *02:15, 1st and 15th
0 0 1 * *Midnight, 1st of month

The most asked-for interval has its own guide: cron every 5 minutes.

Edit and List a Crontab

crontab -e
crontab -l
sudo crontab -u deploy -l

crontab -e opens your crontab in an editor and installs it when you save; jobs run as your user. A step-by-step example is in how to create a cron job.

The System Crontab Has a User Field

# in /etc/crontab or a file in /etc/cron.d/
0 4 * * * root /usr/local/bin/cleanup.sh

Here a sixth field, the user, comes before the command. A line copied from a personal crontab without it fails. On Debian and Ubuntu, cron ignores files in /etc/cron.d whose names contain a dot, so name them like cleanup, not cleanup.cron.

Percent Signs, PATH, and Output

  • Percent signs: cron turns an unescaped % into a new line. Write \%, as in date +\%F.
  • PATH: cron runs jobs with a short PATH, so use full paths or set PATH at the top of the crontab.
  • Shell: jobs run with /bin/sh, not bash, unless you set SHELL.
  • Output: redirect it to a log, or it is mailed to the user or lost.
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

The Time Zone

cron uses the server's time zone, which timedatectl shows. After you change it, restart the cron service (cron on Debian and Ubuntu, crond on AlmaLinux and Rocky Linux) so jobs follow the new zone; see how to change the time zone in Ubuntu. cronie on AlmaLinux and Rocky Linux also reads a CRON_TZ variable that sets a time zone for one crontab; the cron of Debian and Ubuntu does not support it. The background is in what is cron.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations