teluzia.Cloud Engineer · Day 11
CHAPTER 02 · LINUX ADMINISTRATION AND SHELL SCRIPTING

Cron and scheduled tasks

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Make a scheduled command reliable before scheduling it. Cron’s environment is smaller than an interactive shell, and systemd timers use a different model. You will test a script locally, write a reviewed schedule and inject a path failure without installing a recurring task. A traditional user crontab entry has minute, hour, day-of-month, month and day-of-week fields followed by a command. A star accepts every value in that field. Cron interpretation uses the host’s configured time context, and day-field interactions deserve careful review. User and system crontab formats differ. Cron does not inherit your interactive shell aliases or every exported variable. Use absolute paths for executables and files, choose a working directory explicitly, and capture diagnostics. A script should be idempotent when repetition is expected and should avoid overlapping runs when work can take longer than its schedule.

Think of it this way

A schedule is an alarm clock; the script is the task card; logging is proof that someone actually completed the task.

TOPIC 01 / 03Step by step

Cron schedule fields

A traditional user crontab entry has minute, hour, day-of-month, month and day-of-week fields followed by a command. A star accepts every value in that field. Cron interpretation uses the host’s configured time context, and day-field interactions deserve careful review. User and system crontab formats differ. The user entry 0 9 * * 1 runs at 09:00 on Mondays in the cron environment’s time zone, not necessarily your laptop’s travel location. Reading every schedule field explicitly prevents accidental high-frequency runs and makes timing assumptions visible when a command will repeat without a person supervising it. Copying a system crontab line including a username into a user crontab can make that username part of the command and cause unexpected failure.

WORKED EXAMPLE
The user entry 0 9 * * 1 runs at 09:00 on Mondays in the cron environment’s time zone, not necessarily your laptop’s travel location.
Why this matters

Reading every schedule field explicitly prevents accidental high-frequency runs and makes timing assumptions visible when a command will repeat without a person supervising it.

A common trap

Copying a system crontab line including a username into a user crontab can make that username part of the command and cause unexpected failure.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Cron schedule fields

A traditional user crontab entry has minute, hour, day-of-month, month and day-of-week fields followed by a command. A star accepts every value in that field. Cron interpretation uses the host’s configured time context, and day-field interactions deserve careful review. User and system crontab formats differ. The user entry 0 9 * * 1 runs at 09:00 on Mondays in the cron environment’s time zone, not necessarily your laptop’s travel location. Reading every schedule field explicitly prevents accidental high-frequency runs and makes timing assumptions visible when a command will repeat without a person supervising it. Copying a system crontab line including a username into a user crontab can make that username part of the command and cause unexpected failure.

WORKED EXAMPLE
The user entry 0 9 * * 1 runs at 09:00 on Mondays in the cron environment’s time zone, not necessarily your laptop’s travel location.

Why this matters

Reading every schedule field explicitly prevents accidental high-frequency runs and makes timing assumptions visible when a command will repeat without a person supervising it.

A common trap

Copying a system crontab line including a username into a user crontab can make that username part of the command and cause unexpected failure.

Noninteractive environment

Cron does not inherit your interactive shell aliases or every exported variable. Use absolute paths for executables and files, choose a working directory explicitly, and capture diagnostics. A script should be idempotent when repetition is expected and should avoid overlapping runs when work can take longer than its schedule. A reviewed script can call /bin/date and write to "$HOME/teluzia/day11/run.log"; do not rely on cd performed in a previous terminal session. Explicit runtime dependencies make scheduled tasks reproducible and explain why a command that succeeds manually may fail when executed by a minimal unattended environment later. Relying on interactive PATH, aliases or a remembered current directory creates hidden dependencies; test with a minimal environment before installing any recurring job today.

WORKED EXAMPLE
A reviewed script can call /bin/date and write to "$HOME/teluzia/day11/run.log"; do not rely on cd performed in a previous terminal session.

Why this matters

Explicit runtime dependencies make scheduled tasks reproducible and explain why a command that succeeds manually may fail when executed by a minimal unattended environment later.

A common trap

Relying on interactive PATH, aliases or a remembered current directory creates hidden dependencies; test with a minimal environment before installing any recurring job today.

Timers, evidence and overlap

systemd timers activate service units and can use calendar or monotonic schedules. They are inspected with systemctl list-timers when systemd is available. Cron and timer histories should be verified through the actual task output and logs, not merely a valid-looking schedule. Persistent timer behavior is configurable, not universal. Compare a proposed cron line with systemctl list-timers --all --no-pager; observe that timer activation and service execution are separate units with separate evidence. Choosing a scheduler deliberately helps control missed runs, overlap and observability rather than treating every recurring command as equivalent to an interactive terminal invocation today. A schedule existing in configuration does not prove successful completion; collect output, exit status and timestamps before assuming an unattended task is operationally reliable.

WORKED EXAMPLE
Compare a proposed cron line with systemctl list-timers --all --no-pager; observe that timer activation and service execution are separate units with separate evidence.

Why this matters

Choosing a scheduler deliberately helps control missed runs, overlap and observability rather than treating every recurring command as equivalent to an interactive terminal invocation today.

A common trap

A schedule existing in configuration does not prove successful completion; collect output, exit status and timestamps before assuming an unattended task is operationally reliable.

KEEP THESE WITH YOU

Today’s takeaways

Test the command before scheduling it.

Make environment and time assumptions explicit.

Verify completion and prevent overlap where needed.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed