Loading
0x170Lesson 24 of 28

Schedule jobs with cron

Read and write crontab schedules, and avoid the classic cron traps.

20 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Read and write the five fields of a cron schedule
  • Edit a crontab, and capture a job’s output
  • Explain why a script that works by hand can fail in cron

Backups at 2 a.m., log cleanup every Sunday, a report every weekday: cron runs commands on a schedule. Edit your schedule with crontab -e, and list it with crontab -l. Each line has five time fields, then the command:

1┌───── minute (0-59)
2│ ┌─── hour (0-23)
3│ │ ┌─ day of month (1-31)
4│ │ │ ┌ month (1-12)
5│ │ │ │ ┌ day of week (0-7, where 0 and 7 are Sunday)
630 2 * * * /home/crew/backup.sh

* means every value, */15 means every 15, 1-5 is a range and 1,15 is a list.

Try it

Up to 15 XP

Write the schedule

Type a schedule and check the next run times, shown in UTC from Thursday 5 November 2026 at 09:00. A schedule passes when it fires at the right times.

Challenge 1 of 5: Run a backup every day at 02:30.

minute
*
hour
*
day
*
month
*
weekday
*
Next runs (UTC)
  1. Thu, Nov 5, 09:01
  2. Thu, Nov 5, 09:02
  3. Thu, Nov 5, 09:03
  4. Thu, Nov 5, 09:04
  5. Thu, Nov 5, 09:05

Why it worked by hand, but not in cron

Cron runs your command in a bare environment, and this catches everyone:

  • PATH is minimal, so commands might not be found. Use full paths, or set PATH at the top of the crontab.
  • Your .bashrc isn’t loaded, so aliases and variables are missing.
  • The working directory is your home folder, so relative paths may break.
  • Output vanishes unless you capture it: 30 2 * * * /home/crew/backup.sh >> /home/crew/backup.log 2>&1
  • The time zone is the server’s, which is often UTC.

Try it

Up to 15 XP

Spot what will fail

This crontab works perfectly when each command is run by hand. Click what’s likely to fail under cron.

crontab -l (round 1 of 1)Flags found 0/0

Click every suspicious section. There are 3.

# Nightly jobs 0 2 * * * ./backup.sh */5 * * * * ll /var/log > /tmp/list 0 3 * * * /home/crew/sync.sh 0 4 * * 0 /home/crew/rotate.sh >> /home/crew/rotate.log 2>&1

Key takeaways

  • Five fields: minute, hour, day of month, month, day of week.

  • Use full paths, because cron’s environment is bare.

  • Always capture output with >> file 2>&1.

Lesson quiz

5 questions · pass with 4 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Bash scripts

Write a script in the editor and run it for real against sample input. Each run gets a fresh Linux sandbox with Bash 5.2 and the GNU tools on Wandbox, a free public service - so experiment freely, even with rm. Your script and test input are sent there.

Exercise 1

Read the crontab

+25 XP

stdin is a crontab. Skip blank lines and lines starting with #. For each job, print its command (everything after the five time fields). Then print N jobs.

  • Skips comments
  • Empty crontab
script.sh
Loading editor…

Your script runs with Bash 5.2 and GNU tools on Wandbox, a free public service, in a fresh sandbox each time. Your script and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…