What does `wait` return when a background job is killed by a signal?
AdvancedConceptFunctions, exit codes, strict mode and traps
Answer
wait PID returns 128 + signal number, the same convention as a foreground command. If the PID is not a child of this shell, it returns 127 with wait: pid N is not a child of this shell.
sleep 100 & pid=$!
kill -TERM "$pid"
wait "$pid"; echo $? # 143
Note that $! refers to the last background job only; store PIDs in an array as you start jobs.
More on Functions, exit codes, strict mode and traps
How do you define a function, pass arguments and return a value?BeginnerWhat does `local` do, and what is dynamic scoping?AdvancedWhy is `local out=$(cmd)` a problem under `set -e`?AdvancedWhat are the conventional exit codes, and what do 126, 127, 130, 137 and 143 mean?BeginnerWhat does `set -e` do, and where does it NOT cause an exit?AdvancedExplain `set -euo pipefail` line by line, and add the `-E` flag.Beginner