When running a shell job in the background, the shell keeps track of the exit-code that can be retrieved with wait <PID>
.
When a new background job is run, it gets its own new PID, even if previous background jobs are ended, because the exit-code of a background-job is stored and kept as a PID indexed entry, until the parent shell ends.
How many background jobs can be repeatedly run, before the allocated PID counter overflows and overwrites previously stored exit-codes?
Since the stored exit-codes are not released even by wait <PID>
, it appears to cause a memory leak of an ever growing exit-codes store.
Is there a command or option to release a stored background job's exit-code if it is no longer needed?
Here is a test code to show that background-jobs per PID exit-codes are kept stored.
#!/usr/bin/env bash
typeset -a background_jobs
report() {
# wait get exit-code of background job 0
wait "${background_jobs[0]}"
printf 'Exit-code of background job 0 is: 0x%02X\n' $?
# wait get exit-code of background job 1
wait "${background_jobs[1]}"
printf 'Exit-code of background job 1 is: 0x%02X\n' $?
# exit-code of job 0 still not released
wait "${background_jobs[0]}"
printf 'Still stored exit-code of background job 0 is: 0x%02X\n' $?
# exit-code of job 1 still not released
wait "${background_jobs[1]}"
printf 'Still stored exit-code of background job 1 is: 0x%02X\n' $?
local -i job_pid
printf '\nPID Ret\n-------------\n'
for job_pid in "${background_jobs[@]}"; do
wait "$job_pid"
printf '% 8d 0x%02X\n' "$job_pid" "$?"
done
exit 0
}
trap report EXIT
for ((i = 0; i < 25; i++)); do
{
exit $((RANDOM & 16#7F))
} &
background_jobs+=($!)
done 2>/dev/null