Last active
May 30, 2017 13:34
-
-
Save damc-dev/6c26789c81589ef4b254dbc1dd3d696e to your computer and use it in GitHub Desktop.
Run Bash subshells in parallel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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