I have the following, I thought it ran the subshells in parallel, but it actually seems to run them in series, and I cannot figure out why:
#!/usr/bin/env bash
set -e;
set -m # allow for job control
EXIT_CODE=0; # exit code of overall script
function handleJobs() {
for job in `jobs -p`; do
echo "PID => ${job}"
CODE=0;
wait ${job} || CODE=$?
if [[ "${CODE}" != "0" ]]; then
echo "At least one process failed with exit code => ${CODE}" ;
EXIT_CODE=1;
fi
done
}
trap 'handleJobs' CHLD
for file in "$HOME/mongodump_dev/cdt_db/"* ; do
file="$(basename "$file")"
if [[ "$file" != "system"* && "$file" != "locks"* ]]; then
mongorestore \
--db "cdt_dev" \
--collection "${file%.*}" \
--host "<my-host>" \
"$HOME/mongodump_dev/cdt_db/$file" &
fi;
done
wait;
echo "exit code => $EXIT_CODE"
exit "$EXIT_CODE"
Anyone know why the subshells are running in series?
I tried doing this instead:
(
mongorestore \
--db "cdt_dev" \
--collection "${file%.*}" \
--host "<my-host>" \
"$HOME/mongodump_dev/cdt_db/$file" &
) &
now they run in parallel, but now the whole script will never exit, and I fear that I am actually not accurately capturing the exit code.
(...) &, it's clear that it's running in series from the logs and the time it takes, when in parallel, the logs are very different and it's much fasterpsto see how manymongorestoreprocesses are actually running?psis going to tell me if any processes are overlapping in timepsshould show all of them.