Skip to content

Instantly share code, notes, and snippets.

@rms1000watt
Created July 8, 2018 16:55
Show Gist options
  • Save rms1000watt/65d2f1533b0852c3834fcbb69490abeb to your computer and use it in GitHub Desktop.
Save rms1000watt/65d2f1533b0852c3834fcbb69490abeb to your computer and use it in GitHub Desktop.
Batch Parallel cURL Download in Bash
#!/usr/bin/env bash
waitFor() {
pids=$1
errs=()
for pid in ${pids[@]}; do
echo "wait $pid"
wait $pid
errs+=($?)
done
for err in ${errs[@]}; do
echo "$err"
done
}
pids=()
curl "http://urlecho.appspot.com/echo?body=1111%0A" & pids+=($!)
curl "http://urlecho.appspot.com/echo?body=2222%0A" $(sleep 2) & pids+=($!)
curl "http://urlecho.appspot.com/echo?body=3333%0A" $(sleep 3) & pids+=($!)
curl "http://urlecho.appspot.com/echo?body=4444%0A" & pids+=($!)
curl "http://urlecho.appspot.com/echo?body=5555%0A" $(sleep 5) & pids+=($!)
curl "http://urlecho.appspot.com/echo?body=6666%0A" & pids+=($!)
waitFor $pids
pids=()
curl "http://urlecho.appspot.com/echo?body=1111%0A" &
pids+=($!)
curl "http://urlecho.appspot.com/echo?body=2222%0A" $(sleep 2) &
pids+=($!)
curl "http://urlecho.appspot.com/echo?body=3333%0A" $(sleep 3) &
pids+=($!)
curl "http://urlecho.appspot.com/echo?body=4444%0A" &
pids+=($!)
curl "http://urlecho.appspot.com/echo?body=5555%0A" $(sleep 5) &
pids+=($!)
curl "http://urlecho.appspot.com/echo?body=6666%0A" &
pids+=($!)
waitFor $pids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment