Scheduling a task to run automatically

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Scheduling a task to run automatically

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does a scheduler such as cron or Task Scheduler do?
A Runs a command or script at set times without a person starting it
B Runs a script only when a person clicks it
2. Which tool is the built-in task scheduler on Windows?
A Windows Task Scheduler
B cron
3. In a cron expression, what does 30 2 * * * mean?
A Run every 30 minutes for 2 hours
B Run every day at 2:30 am
4. Which cron expression runs a backup at 11:00 pm every day?
A 23 0 * * * /home/backup.sh
B 0 23 * * * /home/backup.sh
5. Why is a scheduler useful for automated backups?
A The backup runs every night even if the user forgets
B The backup is never needed again after the first run

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
10 2 * * * python3 backup.py AA cron entry that runs backup.py automatically at 2:00 am every day
2@reboot /home/user/sync.sh B30 23 * * 5 /home/user/backup.sh
3Run a backup script automatically at 11:30 pm every Friday using a cron expression CA cron entry that runs sync.sh each time the computer starts up
4Windows Task Scheduler can create a daily task that starts a backup program from the command line Dschtasks /create /tn Backup /tr backup.bat /sc daily /st 22:00
52 0 * * * /home/user/backup.sh (meant to run at 2:00 am) EOnly four time fields are given, so cron reads backup.sh as the fifth time field. A cron entry needs five time fields, such as 0 2 * * * backup.sh.
60 2 * * backup.sh FThe minute and hour fields are in the wrong order. This runs at 12:02 am, not 2:00 am. It should be 0 2 * * *.

Definitions

cron

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
# Run backup.sh at 2am every day 0 _ * * * /home/ava/backup.sh
2.
# Run backup.sh once at start-up @______ /home/ava/backup.sh
3.
# Edit your list of scheduled cron jobs _______ -e
4.
# Windows Task Scheduler: run every day schtasks /create /tn "Backup" /sc _____
5.
# Run backup.sh every day at 6:30pm __ 18 * * * /home/ava/backup.sh

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A 0 2 * * /home/ann/backup.sh
B 30 6 * * 1 /home/ann/clean.sh
C @reboot /home/ann/start.sh
Question 2
A # Monthly report, 1st at 9 am
B 0 9 1 * * /home/ann/report.sh
C # Weekly clean, Mon at 5 am
D 0 5 * 1 * /home/ann/clean.sh
Question 3
A crontab -l
B 3 0 * * 0 /home/ann/backup.sh
C # Runs Sunday at 3:00 am
Question 4
A schtask /query /tn Backup
B schtasks /run /tn Backup
C schtasks /end /tn Backup
Question 5
A # Daily backup at 1:15 am
B # min hour day month weekday
C 15 1 1 * * python3 backup.py

Answer Key

Scheduling a task to run automatically
Section A: Quick Questions
Q1
A — Runs a command or script at set times without a person starting it
Q2
A — Windows Task Scheduler
Q3
B — Run every day at 2:30 am
Q4
B — 0 23 * * * /home/backup.sh
Q5
A — The backup runs every night even if the user forgets
Section B: Match the Pairs
Q1
0 2 * * * python3 backup.py → A cron entry that runs backup.py automatically at 2:00 am every day
Q2
@reboot /home/user/sync.sh → A cron entry that runs sync.sh each time the computer starts up
Q3
Run a backup script automatically at 11:30 pm every Friday using a cron expression → 30 23 * * 5 /home/user/backup.sh
Q4
Windows Task Scheduler can create a daily task that starts a backup program from the command line → schtasks /create /tn Backup /tr backup.bat /sc daily /st 22:00
Q5
2 0 * * * /home/user/backup.sh (meant to run at 2:00 am) → The minute and hour fields are in the wrong order. This runs at 12:02 am, not 2:00 am. It should be 0 2 * * *.
Q6
0 2 * * backup.sh → Only four time fields are given, so cron reads backup.sh as the fifth time field. A cron entry needs five time fields, such as 0 2 * * * backup.sh.
Section C: Definitions
Q1
cron — A scheduler that runs a command or script at set times without a person starting it, such as a nightly backup. Windows Task Scheduler does the same job.
Section D: Fill in the Blanks
Q1
# Run backup.sh at 2am every day 0 2 * * * /home/ava/backup.sh
Q2
# Run backup.sh once at start-up @reboot /home/ava/backup.sh
Q3
# Edit your list of scheduled cron jobs crontab -e
Q4
# Windows Task Scheduler: run every day schtasks /create /tn "Backup" /sc daily
Q5
# Run backup.sh every day at 6:30pm 30 18 * * * /home/ava/backup.sh
Section E: Spot the Error
Q1
A — A cron expression needs five time fields (minute, hour, day of month, month, weekday). This line has only four, so cron reads the path as a field. It should be 0 2 * * * before the command.
Q2
D — The fourth field is the month, so this job would only run in January. Monday is set in the fifth (weekday) field, so the line should be 0 5 * * 1.
Q3
B — Cron puts the minute first and the hour second. 3 0 means 12:03 am, not 3:00 am. It should be 0 3 * * 0.
Q4
A — The Windows Task Scheduler command line tool is called schtasks, with an s on the end. The line should be schtasks /query /tn Backup.
Q5
C — The third field is the day of the month. Putting 1 there makes the backup run only on the 1st of each month. Use * so it runs every day: 15 1 * * *.