On Mon, 11 Mar 2024 15:36:48 -0400
Greg Wooledge <g...@wooledge.org> wrote:

> > On Mon, Mar 11, 2024, 20:13 Mischa Baars <mjbaars1977.bac...@gmail.com>
> > wrote:
> > 
> > > Also I don't think that gives you an exit status for each 'exit $i'
> > > started. I need that exit status.
> 
> "wait -n" without a PID won't help you, then.  You don't get the PID or
> job ID that terminated, and you don't get the exit status.  It's only

It does convey the exit status.

> of interest if you're trying to do something like "run these 100 jobs,
> 5 at a time" without storing their exit statuses.

The pid can be obtained with the -p option, as of 5.1. Below is a synthetic 
example of how it might be put into practice.

#!/bin/bash

declare -A job_by status_by
max_jobs=4
jobs=0

wait_next() {
        local pid
        wait -n -p pid
        status_by[$pid]=$?
        unset -v 'job_by[$pid]'
}

worker() {
        sleep "$(( RANDOM % 5 ))"
        exit "$(( RANDOM % 2 ))"
}

for (( i = 0; i < 16; ++i )); do
        (( jobs++ < max_jobs )) || wait_next
        worker & job_by[$!]=
done

while (( ${#job_by[@]} )); do
        wait_next
done

declare -p status_by

-- 
Kerin Millar

Reply via email to