Bash / Editorial solution guide
A Bash batch reports success while a background job fails
Track owned child processes and their wait statuses before reporting a batch outcome.
Read-only knowledge base · No public comments
Recognize the symptoms
A script exits successfully or prints a completion message, but a background task failed or is still running. Output can appear in a different order on each run. Capture the script’s exit status and a minimal reproduction with synthetic child commands before investigating external services.
Understand the likely causes
- The parent may treat successful launch as successful completion, fail to save the child identifier, or wait without checking each child’s status. A broad cleanup command can also affect processes the script does not own.
Reproduce with controlled children
Replace external commands temporarily with one child that exits successfully and another that exits nonzero. Record each $! immediately after starting its child, since later background commands update that parameter. Observe when the parent reports completion and which statuses it checks. Use an isolated test environment; do not kill unrelated processes or use production jobs to provoke a failure.
Wait and aggregate results
The example stores two child identifiers and waits for both. A nonzero wait status sets a failure flag, while the loop continues to reap the other child. The second child exits with status 3, so the displayed flag is 1. For a real script, map the aggregate to a documented nonzero exit status and preserve any per-job context needed to identify which input failed.
pids=()
(exit 0) & pids+=("$!")
(exit 3) & pids+=("$!")
failed=0
for pid in "${pids[@]}"; do
if wait "$pid"; then :; else failed=1; fi
done
printf "failed=%s\n" "$failed"
Each owned PID is waited for; the nonzero child sets the aggregate failure flag.
Handle interruption and partial effects
Define whether a failed job stops new work or whether already started jobs continue. On interruption, signal and wait for owned children according to a tested policy, taking process groups and possible descendants into account. A failed or interrupted child can leave a partial external effect; wait does not roll it back. Check the target system and apply its documented recovery or idempotency contract before retrying.
Verify all completion paths
Test all-success, one-failure, and a slow child completing after a fast failure. Assert that the parent does not announce success before every owned child has settled and that its exit status matches the batch contract. Exercise interruption in an isolated environment and check that unrelated processes remain untouched. Do not assert a particular order of child output unless the program intentionally serializes it.
Verify the fix
Confirm both children are waited for, the failure flag is set, and the real script returns its documented nonzero status. Repeat with all children successful and with a delayed child.
Keep it from recurring
- Track explicit child identifiers, bound active work, and test error and interruption paths. Document recovery for partial external effects before enabling retries.