Skip to content

Instantly share code, notes, and snippets.

@mdouchement
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdouchement/b57278efdd37d113158a to your computer and use it in GitHub Desktop.
Save mdouchement/b57278efdd37d113158a to your computer and use it in GitHub Desktop.
Bash: synchronisation barrier
#!/bin/bash
./process_1.sh &
pid_1=`echo $!`
./process_2.sh &
pid_2=`echo $!`
./process_3.sh &
pid_3=`echo $!`
# synchronisation barrier
wait $pid_1 $pid_2 $pid_3
./process_4.sh

Bash barrier with all exit status

Demo example

In statement tasks="(sleep $i; true && echo ok) & ${tasks}":

  • The boolean true represents command execution that finish by exit 0
  • You can test with true; echo $?
  • { (sleep 3; true && echo ok) & (sleep 2; true && echo ok) & } | grep -c ok | ( read n && (( n==2 )) ) ; echo $?
  • A boolean false in this example represents command execution that finish by exit 1 (other exit code)
  • You can test with false; echo $?
  • { (sleep 3; true && echo ok) & (sleep 2; false && echo ok) & } | grep -c ok | ( read n && (( n==2 )) ) ; echo $?
i=0
tasks=''

for engine in 1 2 3
do
  i=$(($i+1))

  tasks="(sleep $i; true && echo ok) & ${tasks}"
done

echo "Executed command: eval '{ ${tasks} } | grep -c ok | ( read n && (( n==${i} )) ) ; echo \$?'"

# synchronisation barrier with exitstatus ckeck
exitstatus=$(eval "{ ${tasks} } | grep -c ok | ( read n && (( n==${i} )) ) ; echo \$?")
echo $exitstatus

Concrete example

  • Launch several ruby tests and verify that all pass:
#!/bin/bash

i=0
tasks=''

cd engines
for engine in 1 2 3
do
  i=$(($i+1))

  tasks="(cd `bundle show $engine`; bundle exec rspec && echo ok) & ${tasks}"
done

echo "Executed command: eval '{ ${tasks} } | grep -c ok | ( read n && (( n==${i} )) ) ; echo \$?'"

# synchronisation barrier with exitstatus ckeck
exitstatus=$(eval "{ ${tasks} } | grep -c ok | ( read n && (( n==${i} )) ) ; echo \$?")
echo $exitstatus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment