Scheduled automation environments
Scheduled scripts may start with a different working directory, PATH, locale, HOME, interpreter and permissions from an interactive terminal. They may have no terminal and must not depend on aliases, prompts or personal startup files. Define an explicit interpreter, trusted executable search path, working directory, needed inputs and outputs. Decide where stdout, stderr and exit statuses are recorded. Use UTC timestamps where appropriate and monitor failures rather than assuming a job ran.
Scheduling is external to Bash
Cron traditionally uses five time fields followed by a command in a user crontab; system crontabs can add a user field. For example, 15 3 * * * /bin/bash /path/to/report.sh is illustrative only: the path, timezone, account, logging and environment must be chosen for the target. A systemd timer typically activates a matching service with a defined ExecStart and WorkingDirectory. Calendar timers can use OnCalendar; Persistent=true can trigger a missed calendar run after activation, not replay every missed occurrence. Consult the Linux administration track for authorized scheduler setup. No scheduler is installed, enabled or queried here.
Test the script directly with a minimal environment, a different cwd and predictable locale. Use a lock if overlapping invocations would conflict, and bounded execution for long tasks. Make retries safe. A scheduler launch does not mean the underlying task succeeded; the process status and report need independent monitoring. The reduced environment example omits HOME intentionally because its task does not use it. A real scheduled task that needs HOME should receive an explicit trusted value. A failed setup must stop the working-directory test.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Run with an explicit environment
export BM_DEMO=interactive/usr/bin/env -i PATH=/usr/bin:/bin LC_ALL=C /bin/bash --noprofile --norc -c ' printf "mode=%s\n" "${BM_DEMO:-scheduled}"' Expected stdout:
mode=scheduledThe parent variable is absent from the reduced environment; this is a scheduler simulation, not isolation.
Resolve an explicit working directory
mkdir task elsewhere || exit 1printf 'fixture\n' > task/input.txt || exit 1base=$PWD/task( cd elsewhere || exit 1 cd -- "$base" || exit 1 cat input.txt)Expected stdout:
fixtureThe task does not depend on the launch directory.
Generate a deterministic UTC timestamp
Expected stdout:
1970-01-01T00:00:00ZGNU date converts the fixed epoch input rather than using current time.
Common mistakes
A script that works from your terminal may fail in cron. Do not assume an interactive HOME or working directory. A scheduled job should not prompt for a password or depend on a terminal.
Practice
Run the mock task with a minimal environment and from another fixture directory. Define how a failure is reported and how overlap is prevented. Draft a scheduler plan without installing it.
Next: Idempotence and safe updates