Scheduling and Job Automation

Use an understood worker, explicit environment, overlap policy, and visible outcomes.

Choose the scheduler deliberately

Interface Typical role
cron Recurring calendar schedule
at One-time queued work
systemd timer Service activation with manager observability

A scheduler does not validate the worker. A successful submission does not prove later completion.

Persistent installation is separate from inspecting an owned schedule file.

Cron fields describe a policy

15 2 * * * /bin/bash /home/STUDENT/thinkbash/ch16/worker.sh

Minute 15, hour 2, every day/month/weekday. Replace STUDENT on the persistent VM before a manual authorized installation.

System crontab files have an additional user field. Timezone/DST and implementation rules matter. Keep percent-containing date formats in a wrapper rather than ignoring cron's percent semantics.

Do not edit or delete a real schedule incidentally

crontab -l is inspection. Editing, installing a replacement file, and removal change account state.

Back up the existing schedule, review the proposed merge, and establish restoration before an authorized change. Do not use an implementation-specific interactive flag as a generic installation recipe.

The executable lesson does not replace a crontab or submit an at job.

Verify one owned runtime timer

unique user timer -> bounded echo worker -> known journal marker
                 -> bounded polling -> stop/reset owned units

Verified on course systemd 249 and development systemd 255. No persistent timer is enabled.

OnActiveSec is relative to activation; OnCalendar is a calendar policy. Trigger accuracy is not a hard real-time guarantee.

Simulate the unattended environment

env -i PATH=/usr/bin:/bin /bin/bash "$worker" "$workspace"

Use the complete lesson setup. Absolute executable paths and an explicit workspace make the contract independent of an interactive directory and aliases.

Capture stdout, stderr, status, and resulting files. Do not repurpose HOME to impersonate another account.

Lock before work; clean up only your lock

The supplied worker acquires an owned directory lock atomically. A held lock returns 75 and produces no result. The successful acquirer registers cleanup.

A crash can leave a stale directory. Inspect the original worker, ownership, and context before recovery; a failed acquirer must not remove another caller's lock.

This is a local fixture, not a distributed locking guarantee.

Preserve argument boundaries

printf '%s\0' 'east host' '*.log' |
  xargs -0 -n 1 /bin/bash -c 'printf "<%s>\n" "$1"' _

Expected: <east host>, then <*.log>.

The dummy argument becomes $0. Pass items as arguments, never splice them into shell program text.

Bound parallelism and reconcile failures

-P 2 permits at most two child invocations. Output order is not execution order.

Use one result per controlled task ID, wait for xargs, inspect aggregate status, and reconcile every item. A failed aggregate does not mean no tasks completed.

Guard zero-byte item files explicitly because empty-input defaults vary across implementations.

Consolidate on your VM

The graded homework applies this: a batch publisher with a lock and bounded parallelism.

Verify reduced environment, overlap refusal, visible failure, recovery, bounded concurrency, and all task results. Separate tested runtime scheduling from manual persistent cron/at installation.

Sources: Linuxize cron, systemd timer, and xargs references linked in the lessons. All executable fixtures are owned and bounded.