A check `procs=$(ps aux | grep -v grep | grep -c '[n]ginx: worker')` kills the script under `set -euo pipefail` whenever nginx is down, before the alert code runs.
IntermediateTroubleshootingTroubleshooting: strict mode, exit codes, pipelines and traps
Answer
Likely causes → grep exits 1 when it matches nothing, and grep -c exits 1 even while printing 0. With pipefail the pipeline fails, the assignment inherits that status, and set -e exits, so the "nginx is down" branch is never reached. "No match" is not an error here, but strict mode cannot know that.
Diagnose →
echo foo | grep -c bar; echo "rc=$?" # prints 0, rc=1
Fix → Tell the shell that "no match" is an expected outcome, or use a tool that does not treat it as failure:
procs=$(pgrep -c -f 'nginx: worker' || true) # pgrep exits 1 on no match too
procs=$(ps -eo args | awk '/^nginx: worker/ {n++} END {print n+0}') # awk exits 0 either way
if (( procs == 0 )); then alert CRITICAL "nginx has no workers"; fi
Avoid || true on a whole long pipeline, because it also hides real failures such as ps itself breaking.
Prevent → Treat grep, diff, cmp and pgrep as "exit 1 is a valid answer" tools and handle that status explicitly.
More on Troubleshooting: strict mode, exit codes, pipelines and traps
A script with `set -e` stops silently right after `((retries++))` on its first pass. No error message, exit code 1.Intermediate`notify.sh: line 9: SLACK_WEBHOOK: unbound variable` — but the webhook is meant to be optional.BeginnerOn a CentOS 7 / Amazon Linux 2 host, `deploy.sh "${extra_args[@]}"` fails with `extra_args[@]: unbound variable`. It works on your laptop.Advanced`local release=$(curl -fsS "$API/latest")` fails with a 503, yet the function happily continues and deploys an empty release name.IntermediateA script that does `first=$(sort -u huge.txt | head -n 1)` exits with status 141 under `pipefail`, but only on large inputs.AdvancedYou added `trap 'echo "failed at line $LINENO" >&2' ERR`, but when a command inside a function fails, nothing is printed and the script just exits.Intermediate