Scheduling Tasks with cron

By the end of this lesson you'll be able to schedule commands to run automatically — every night, every 15 minutes, or once at startup — using cron's five schedule fields, special shortcuts, and a crontab that actually runs reliably.

Learn Scheduling Tasks with cron in our free Command Line course — an interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Cli course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn

1. The crontab: -e, -l and -r

Your personal schedule lives in a file called your crontab , but you never open that file by hand — you manage it with three commands. crontab -e edits it (this is how you add jobs), crontab -l lists it, and crontab -r removes it. Be careful: crontab -r deletes your entire crontab with no confirmation, and it's one key away from -e — a classic footgun. Back up first with crontab -l > backup.txt .

2. The Five Schedule Fields

Every cron line is five time fields followed by the command. The order, left to right, is always: minute (0-59), hour (0-23), day-of-month (1-31), month (1-12), and day-of-week (0-7, where both 0 and 7 mean Sunday). So 30 2 * * * means minute 30, hour 2, any day, any month, any weekday — i.e. 02:30 every day .

3. Field Syntax: *, Steps, Ranges & Lists

Each field accepts four building blocks. * means every value. */n is a step — */5 in the minute field means every 5 minutes. a-b is a range — 1-5 in day-of-week is Monday through Friday. And a,b,c is a list — 1,15,30 . Combine them to express almost any schedule you need.

4. Special Shortcuts: @daily, @reboot & Friends

For common schedules, cron offers shortcuts that replace all five fields. @reboot runs a job once when the system boots; @hourly , @daily , @weekly , @monthly , and @yearly are readable aliases — for example @daily is exactly 0 0 * * * . You still add the command after the shortcut.

5. Output, Email & Logging

By default, cron emails whatever a job prints (stdout and stderr) to the local user. Put MAILTO="you@example.com" at the top of the crontab to choose the recipient, or MAILTO="" to switch that mail off. More commonly, people send output to a log file by appending >> /var/log/task.log 2>&1 — where 2>&1 routes errors into the same log so nothing is lost.

6. cron's Minimal Environment (use absolute paths!)

This is the single most common reason a cron job "works in my shell but not in cron." cron runs with a minimal environment : a sparse PATH (often just /usr/bin:/bin ) and none of your ~/.bashrc or login settings. So always use absolute paths to your commands and scripts — /usr/bin/python3 /home/user/app/job.py — or set PATH= at the top of the crontab. As a fast contrast: at runs a command once at a future time, and systemd timers are a modern alternative on systemd-based Linux.

🎯 Your Turn: schedule a daily 2:30 AM backup

Fill in the two blanks marked ___ using the # 👉 hints, then read the line back left to right and check it matches the Output panel.

🎯 Your Turn: run a check every 15 minutes

Two blanks again: pick the step syntax for "every 15 minutes," and the redirect that appends output to a log. Then check the Output panel.

Pro Tips

Common Errors (and the fix)

📋 Quick Reference

Concept

Syntax

Notes

Edit crontab

crontab -e

Add or change jobs

List crontab

crontab -l

Print your jobs

Remove crontab

crontab -r

Deletes ALL of it

Field order

min hr dom mon dow

Then the command

Every value

*

Matches all

Step

*/5

Every 5 (e.g. minutes)

Range

1-5

Mon-Fri in dow

List

1,15,30

Specific values

Daily shortcut

@daily

= 0 0 * * *

At boot

@reboot

Runs once at startup

Email output

MAILTO="..."

Empty = no mail

Log output

>> file 2>&1

Append out + errors

Frequently Asked Questions

Mini-Challenge: a weekday morning report

No blanks this time — just a brief and an outline. Build the full cron line that runs a report script at 8:00 AM on weekdays and logs its output. Plan each field in order, use the absolute path, and check your line against the target in the comments.

🎉 Lesson Complete!

Practice quiz

What is the order of the 5 cron schedule fields?

  • minute hour day-of-month month day-of-week
  • minute hour month day-of-month day-of-week
  • second minute hour day month
  • hour minute day-of-month month day-of-week

Answer: minute hour day-of-month month day-of-week. The five fields, in order, are: minute, hour, day-of-month, month, day-of-week, then the command.

How do you edit your own crontab?

  • crontab -r
  • cron edit
  • crontab -e
  • crontab -l

Answer: crontab -e. crontab -e opens YOUR crontab in an editor; -l lists it and -r removes it.

What does */5 in the minute field mean?

  • From minute 5 onward
  • Every 5 minutes
  • Only on the 5th
  • At 5 minutes past the hour

Answer: Every 5 minutes. The step syntax */5 in the minute field means run every 5 minutes.

What does the line 0 9 * * 1-5 do?

  • 9 PM every day
  • Every 9 minutes on weekdays
  • At minute 9 every hour
  • At 9 AM Monday through Friday

Answer: At 9 AM Monday through Friday. minute 0, hour 9, any day-of-month, any month, days 1-5 (Mon-Fri) means 9:00 AM on weekdays.

What does @reboot do in a crontab?

  • Runs the job once at system startup
  • Runs the job every minute
  • Disables the job
  • Reboots the machine on a schedule

Answer: Runs the job once at system startup. @reboot runs the job a single time when the system boots, not on a clock schedule.

Which value range is correct for the hour field?

  • 1-12
  • 0-23
  • 1-24
  • 0-12

Answer: 0-23. Hours run 0-23 in 24-hour form; 0 is midnight and 23 is 11 PM.

Why do cron jobs often fail even though the same command works in your shell?

  • cron only runs as root
  • cron cannot run shell scripts
  • cron caps jobs at one per hour
  • cron has a minimal environment and PATH, so use absolute paths

Answer: cron has a minimal environment and PATH, so use absolute paths. cron runs with a sparse PATH and not your login environment, so commands and scripts should use absolute paths (or set PATH at the top of the crontab).

What does the MAILTO setting in a crontab do?

  • Schedules an email job
  • Logs output to a file
  • Sets the email address that receives cron output
  • Sends a test email immediately

Answer: Sets the email address that receives cron output. MAILTO=you@example.com sets who receives a job's stdout/stderr; MAILTO= (empty) disables that mail.

Which schedule runs at midnight on the 1st of every month?

  • 0 0 * * 1
  • 0 0 1 * *
  • * * 1 * *
  • 0 1 0 * *

Answer: 0 0 1 * *. minute 0, hour 0, day-of-month 1, any month, any weekday means 00:00 on the 1st of each month.

Which command schedules a one-time job to run once at a future time?

  • at
  • crontab -r
  • cron once
  • watch

Answer: at. at runs a command a single time at a future moment; cron is for jobs that repeat on a schedule.

Continue this course

Related lessons