Skip to content

Instantly share code, notes, and snippets.

@damc-dev
Last active May 30, 2017 13:34
Show Gist options
  • Save damc-dev/6c26789c81589ef4b254dbc1dd3d696e to your computer and use it in GitHub Desktop.
Save damc-dev/6c26789c81589ef4b254dbc1dd3d696e to your computer and use it in GitHub Desktop.
Run Bash subshells in parallel
#!/bin/bash
TMP_DIR="$(mktemp -d -t subshells.XXXXXXXXXX)"
function cleanup {
rm -rf "${TMP_DIR}"
}
trap cleanup EXIT
PIDS=()
for i in 1 2 3; do
(
echo "$(date)" >> "${TMP_DIR}/${BASHPID}.out"
) &
PID="$!"
PIDS+=("${PID}")
echo "$$ started ${PID}"
done
for PID in ${PIDS[@]}; do
wait ${PID}
cat "${TMP_DIR}/${PID}.out"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment