Skip to content

Instantly share code, notes, and snippets.

@dishbreak
Created March 26, 2019 21:34
Show Gist options
  • Save dishbreak/375893c8a0750cd3673af8f48efe49db to your computer and use it in GitHub Desktop.
Save dishbreak/375893c8a0750cd3673af8f48efe49db to your computer and use it in GitHub Desktop.
Executing Multiple Subprocesses in Parallel
#!/usr/bin/env bash
# kill all subprocesses when the user presses CTRL + C
trap "kill 0" SIGINT
# exit on first failed command (good practice)
set -e
echo "Starting"
# The ampersand will execute the given shell as a subprocess.
( echo "Starting 3"; sleep 3; echo "waited for 3") &
( echo "Starting 2"; sleep 2; echo "waited for 2") &
( echo "Starting 1"; sleep 1; echo "waited for 1") &
# Waits for all subshells to complete
wait
echo "Done"
# Output:
# Starting
# Starting 3
# Starting 2
# Starting 1
# waited for 1
# waited for 2
# waited for 3
# Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment