brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: wait -n continues a shell script when the first background job ends

Shell
~/tips/tip-shell-wait-n-first-job

wait -n

Resumes the script as soon as any one background job has finished.

Sometimes I want the fastest answer, or I want to refill a job slot as soon as one finishes.

for host in a b c; do ping -c1 "$host" >/dev/null & done
wait -n
echo "one job finished"

wait alone waits for every background job. wait -n returns when the next one ends and passes on its exit status, which wait cannot do for several jobs. It needs bash 4.3 or newer. In a loop, run wait -n each time the number of running jobs reaches your limit to build a small job pool.

Further reading: Bash manual: Job Control Builtins

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link