Computing Foundations › The Command Line › Day 14

Day 14: Automating Tasks with Shell Scripts and cron

Day 14 of 365 — Automating Tasks with Shell Scripts and cron

After this lesson you will be able to turn a repetitive chore into a safe, logged shell script and read a cron schedule field by field — so you can decide what to automate, when to run it, and how to keep it trustworthy while nobody is watching.

Course
Computing Foundations
Category
The Command Line
Reading time
≈ 40 min
Practical time
≈ 30 min
Lesson duration
1h 10m
Last verified
2026-07-12

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/computing-foundations/day-014-automating-tasks-with-shell-scripts-and

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/computing-foundations/day-014-automating-tasks-with-shell-scripts-and
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters


The idea in plain language

Diagram: your interactive shell compared with cron's minimal environment


Historical background


What it is — and what it is not

Diagram: a job that is unsafe to repeat compared with one that is idempotent

Diagram: the stages of a safe unattended automation

It is:

It is not:


Why it was created and what problems it solves


How it works

Reading a crontab, field by field

Diagram: the five crontab fields decoded, with the example schedule 0 9 star star 1 meaning 9am every Monday

PositionFieldValues* means
1Minute0–59every minute
2Hour0–23every hour
3Day of month1–31every day
4Month1–12 or janevery month
5Day of week0–7 (0 and 7 = Sunday)every weekday

From a run to a logged, safe job

Flowchart: a scheduler triggers a script, the script runs and logs its output, then success or failure is handled

Redirecting the output

0 9 * * 1 /usr/local/bin/weekly-report.sh >> /var/log/weekly.log 2>&1

An everyday analogy

Diagram: overlapping scheduled runs compared with a lock file preventing them

An alarm clock, not an assistant.

The alarm clockcron
The time you setThe five fields
It rings whether you are there or notThe job starts regardless
It does not check you got upNo monitoring of the result
It does not ring again if the power was outA missed run stays missed
A note beside the bedThe log file

Examples in practice

Diagram: three cron schedules decoded field by field


Implications: security, privacy, performance, scalability, and cost

Diagram: the stages by which an unwatched scheduled job fails unnoticed


Alternatives: free, open source, and commercial

Diagram: the fields an unattended job's log needs

ToolTypeWhere it fits
cronFree, built inOne machine, simple schedules, universal availability
systemd timersFree, built inLinux, when you want logging and dependencies
launchdFree, built inmacOS, including jobs that run on events rather than times
atFree, built inA single run, once, at a stated time
GitHub Actions schedulesFree tierJobs that belong with the repository
Airflow, Dagster, PrefectFree, open sourceReal pipelines: dependencies, retries, backfills

TermWhat it actually is
cronThe daemon that launches jobs on a schedule
crontabThe table of rules, one line per job
JobOne schedule paired with one command
IdempotentSafe to run more than once
DaemonA background process with no terminal
OrchestratorA scheduler that also understands dependencies

When to use it — and when not to

Diagram: cron compared with modern schedulers


The AI connection

Diagram: the checks a script should pass before being scheduled


Knowledge check

quiz:
  question: "A script runs perfectly when you execute it yourself, but produces nothing when cron runs it at the scheduled time. What is the most likely cause?"
  options:
    - "cron provides a minimal environment, so a command or path the script relied on is not found"
    - "The crontab line has the minute and hour fields in the wrong order"
    - "cron cannot execute shell scripts without the interpreter named explicitly"
    - "The script needs administrator rights that cron does not grant"
  answer_index: 0
  explanation: "cron reads none of your shell configuration. Its PATH is short, its working directory is your home rather than your project, and no virtual environment is active — so a command that resolved fine in your terminal is simply not found."

Hands-on exercise

Schedule a job, make it fail, then find out why from its log. Worked through fully in the Day 14 lab directory.

What you are doingCommand
See what cron seesenv -i /bin/sh -c 'echo $PATH'
Write a logging scriptecho "[$(date)] ran" >> "$HOME/lab.log"
Edit your schedulecrontab -e
Schedule it every minute* * * * * /full/path/job.sh >> /full/path/job.log 2>&1
Watch the log filltail -f ~/job.log
List and then remove itcrontab -l then crontab -e

Expected output

/usr/bin:/bin
[Sun Aug 30 14:03:00 2026] ran
[Sun Aug 30 14:04:00 2026] ran
/bin/sh: brew: command not found

Validate your work

Troubleshooting

Common mistakes


Practice assignment


Extension challenge


Quiz

Q1. In a standard crontab line, what are the five time fields in order?

  1. Hour, minute, day of month, month, day of week
  2. Minute, hour, day of week, day of month, month
  3. Second, minute, hour, day, month
  4. Minute, hour, day of month, month, day of week
Show answer

Answer: D. Minute, hour, day of month, month, day of week

A crontab line is minute, hour, day-of-month, month, day-of-week, followed by the command. Because the first field is minutes (not hours), reading the fields smallest-unit-first is the reliable way to avoid the most common cron mistake.

Q2. What does the schedule `0 9 * * 1` mean?

  1. At 09:00 every Monday
  2. Every 9 minutes on Mondays
  3. At 9 a.m. on the 1st of every month
  4. At minute 0 of every hour on the 9th
Show answer

Answer: A. At 09:00 every Monday

Minute 0, hour 9, any day of month, any month, and day-of-week 1 (Monday). The two star fields remove any calendar-date restriction, so it runs at nine o'clock sharp every Monday.

Q3. Why must a script that runs unattended on a schedule write to a log file?

  1. Log files make the script run faster
  2. At 3 a.m. nobody is watching; the log is the only record
  3. cron refuses to run any script that does not log
  4. Logging is required to convert bytes to gigabytes
Show answer

Answer: B. At 3 a.m. nobody is watching; the log is the only record

A scheduled job runs while no one is watching, so without a log a failure is silent until someone notices the missing result. The log is the unattended job's only way to report what it did and whether it succeeded.

Q4. What does it mean for a task to be idempotent?

  1. It runs faster each time it is repeated
  2. It can only ever be run a single time
  3. Running it twice does no more harm than running it once
  4. It requires administrator privileges to run
Show answer

Answer: C. Running it twice does no more harm than running it once

An idempotent job checks the current state before acting, so re-running it is harmless. This matters for scheduled jobs, which may fire twice or run on already-processed data — a non-idempotent job could double-apply and corrupt data.

Q5. What is the purpose of a dry-run mode in a script that moves or deletes files?

  1. It makes the file operations permanent and irreversible
  2. It compresses the files before moving them
  3. It schedules the script to run later with cron
  4. It prints what the script would do without changing anything
Show answer

Answer: D. It prints what the script would do without changing anything

A dry run prints the intended actions and changes nothing, letting you verify a destructive script before trusting it. Building the dry-run mode first is the habit that protects your data.

Q6. On modern macOS, which scheduler is the recommended tool, with cron deprecated though still present?

  1. launchd
  2. Windows Task Scheduler
  3. systemd timers
  4. at
Show answer

Answer: A. launchd

Apple introduced launchd in 2005 and it is now the recommended scheduler on macOS. It has the practical advantage of catching up on jobs that were missed while the Mac was asleep, which cron never runs.

Q7. A colleague says "the cron job is broken." What is usually the real cause?

  1. cron itself has crashed and must be reinstalled
  2. A bug in the script or its environment; cron only launches it
  3. The schedule fields were written in the wrong century
  4. The computer has run out of transistors
Show answer

Answer: B. A bug in the script or its environment; cron only launches it

cron only launches the command at the scheduled time; it does not understand or retry the job. Most "cron problems" are ordinary script bugs — often a relative path or missing environment variable — that reproduce when you run the command by hand.

Q8. Which tool is designed to run a command once at a single future time rather than on a repeating schedule?

  1. crontab
  2. launchd
  3. at
  4. systemd timer
Show answer

Answer: C. at

The `at` command schedules a one-off job for a single future time, such as `echo "command" | at 20:00`. cron and the other schedulers are for recurring jobs; `at` is for "just once, later."

Q9. Why should you quote variables such as `"$file"` in a scheduled shell script?

  1. Quoting makes cron run the job on time
  2. Quoting converts the file to a different format
  3. Unquoted variables are automatically deleted by the shell itself
  4. A filename with a space would break the command into pieces
Show answer

Answer: D. A filename with a space would break the command into pieces

cron runs commands through the shell, which splits unquoted values on spaces. Quoting every variable keeps a filename like "my report.pdf" as a single argument, so a space or special character cannot turn the command into something you did not intend — a safety essential for unattended jobs.

Glossary

automation
Pairing a script that performs a task with a scheduler that runs it at chosen times, so the work happens reliably without a person doing it by hand.
cron
The classic Unix scheduler: a background program that wakes every minute, checks a table of jobs against the current time, and runs the ones that match.
crontab
The cron table — a per-user list of scheduled jobs, one job per line; edited with crontab -e and viewed with crontab -l.
cron expression
The five whitespace-separated time fields at the start of a crontab line — minute, hour, day-of-month, month, day-of-week — that say when a job runs.
scheduled task
A command or script set to run automatically at specified times or intervals by a scheduler, rather than being started by hand.
launchd
The service manager and scheduler on macOS, introduced in 2005 and now the recommended tool there; it can also run jobs that were missed while the Mac was asleep.
systemd timer
A scheduling unit on most Linux distributions that runs a paired service on a schedule, managed and logged like any other systemd service.
idempotent
A property of a task that makes running it more than once no more harmful than running it once, because it checks the current state before acting.
dry run
A mode in which a script prints the actions it would take without performing any of them, so you can safely preview a destructive job before trusting it.
logging
Writing a record of what a program did — usually timestamped lines appended to a file — which is essential for unattended jobs that have no screen to watch.
at
A Unix command that runs a job once at a single future time rather than on a recurring schedule, for one-off delayed or reminder tasks.
Task Scheduler
The built-in scheduler on Microsoft Windows, used through its graphical tool or commands like schtasks to run programs at set times.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.