To maximize CPU usage (I run things on a Debian Lenny in EC2) I have a simple script to launch jobs in parallel:
#!/bin/bash
for i in apache-200901*.log; do
I had to do this recently and ended up with the following solution:
while true; do
wait -n || {
code="$?"
([[ $code = "127" ]] && exit 0 || exit "$code")
break
}
done;
Here's how it works:
wait -n exits as soon as one of the (potentially many) background jobs exits. It always evaluates to true and the loop goes on until:
127: the last background job successfully exited. In
that case, we ignore the exit code and exit the sub-shell with code
0.With set -e, this will guarantee that the script will terminate early and pass through the exit code of any failed background job.