Sub topic 7
Exit status: making failures loud
This chapter assumes you are comfortable with shell commands, pipes, basic scripting, Linux processes, and reading logs. You will use those skills to make a scheduled job report failure instead of appearing successful.
An exit status is a number a command returns when it finishes. By convention, 0 means success. Any nonzero value means failure. A scheduler can use this value to decide whether a run succeeded, but only if your script preserves and exposes it.
How exit statuses work
The special shell variable $? contains the status of the most recently completed command.
#!/usr/bin/env bash
if /usr/local/bin/sync-data; then
echo "Sync completed"
else
status=$?
echo "Sync failed with status $status" >&2
exit "$status"
fiThe if statement checks the command directly. The script writes the error to standard error, then exits with the same nonzero value. A scheduler can now record the run as failed.
Do not put an unrelated successful command after a command that failed:
/usr/local/bin/sync-data
echo "Finished"Without additional safeguards, the script may continue and return the status from echo, which is normally zero.
Make shell scripts stop and report
For many jobs, begin with strict shell options:
#!/usr/bin/env bash
set -Eeuo pipefail
log() {
printf '%s %s\n' "$(date --iso-8601=seconds)" "$*" >&2
}
log "Starting import"
/usr/local/bin/download-data
/usr/local/bin/import-data
log "Import completed"These options have different purposes:
| Option | Effect |
|---|---|
| `-e` | Exits when an unhandled command fails |
| `-u` | Treats an unset variable as an error |
| `pipefail` | Makes a pipeline fail if any command in it fails |
-E preserves error traps in functions and command substitutions. Strict mode is useful, but set -e has exceptions. Commands used in if, while, until, and some boolean expressions may fail without ending the script. Check expected failures explicitly.
A pipeline can hide an error unless pipefail is enabled:
set -o pipefail
curl -fsS https://example.test/data | gzip > /var/tmp/data.gzHere, curl fails for an unavailable URL, and the pipeline returns failure instead of reporting only whether gzip succeeded.
How schedulers notice failure
A cron job generally indicates failure through its output, often by local mail, but the exact delivery depends on the system's mail configuration. Redirecting output to a log preserves evidence:
*/15 * * * * /usr/local/bin/sync.sh >> /var/log/sync.log 2>&1A systemd service records a nonzero ExecStart status as a failed unit. Inspect it with:
sudo systemctl status backup.service
sudo journalctl -u backup.service --since todayThe important chain is:
Preserve useful failure information
Use trap when you need a consistent error message:
#!/usr/bin/env bash
set -Eeuo pipefail
trap 'printf "failed: line %s, status %s\n" "$LINENO" "$?" >&2' ERR
/usr/local/bin/prepare
/usr/local/bin/publishThe trap reports the line and status before the script exits. Avoid catching an error merely to print a message and then returning success. If a failure matters, the final exit status must remain nonzero.
Exercise
What should this script return when backup-part-1 fails, and why? backup-part-1 && backup-part-2
Show answer
It returns the nonzero status from backup-part-1, and backup-part-2 does not run because && runs the next command only after success.