Schedule jobs with cron
Read and write crontab schedules, and avoid the classic cron traps.
- 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 XPWrite 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
- *
- Thu, Nov 5, 09:01
- Thu, Nov 5, 09:02
- Thu, Nov 5, 09:03
- Thu, Nov 5, 09:04
- 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 XPSpot what will fail
This crontab works perfectly when each command is run by hand. Click what’s likely to fail under cron.
Click every suspicious section. There are 3.
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.
Read the crontab
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
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…