A nightly job should post to Slack when it starts, succeeds or fails, including the duration and the last 20 log lines on failure. Design it.
IntermediateScenarioScenarios: writing and hardening scripts
Answer
Situation read → Notifications must work even when the job crashes, so they belong in traps, not at the end of the happy path.
What I'd do →
log=$(mktemp); exec > >(tee -a "$log") 2>&1
start=$SECONDS
notify() { jq -n --arg t "$1" '{text: $t}' |
curl -fsS --max-time 10 -H 'Content-Type: application/json' --data-binary @- "$SLACK_WEBHOOK" >/dev/null || true; }
finish() {
local rc=$?
if (( rc == 0 )); then notify "backup OK in $((SECONDS - start))s"
else notify "backup FAILED (exit $rc) after $((SECONDS - start))s"$'\n'"$(tail -n 20 "$log")"; fi
rm -f -- "$log"
}
trap finish EXIT
notify "backup started on $(hostname -s)"
- Build JSON with
jqso log lines with quotes cannot break the payload. - Notification failures must never fail the job (
|| true).
Why → The EXIT trap sees the real exit code in all cases, including set -e aborts.
Interview tip → Mention avoiding alert fatigue: notify on failure and on recovery, and keep "started" messages for jobs people actively watch.
More on Scenarios: writing and hardening scripts
You inherit a 600-line `deploy.sh` with no error handling. It "sometimes half-deploys". Where do you start?IntermediateA script with `#!/bin/sh` works on CentOS but fails on Ubuntu with `Syntax error: "(" unexpected`. What is going on and what do you do?BeginnerYou need a deploy script that humans and CI both call with `--env`, `--version` and `--dry-run`. How do you design its interface?BeginnerA maintenance script must run as the `deploy` user, never as root, because files it creates must be owned by `deploy`. How do you enforce that?BeginnerTen scripts in a repo each copy-paste their own `log`, `die`, retry and config loading. How do you fix this?IntermediateA script is run both by engineers and by cron. Cron logs are full of colour codes and the script sometimes hangs waiting for input. Fix it.Beginner