Skip to content

Instantly share code, notes, and snippets.

@dasgoll
Last active February 8, 2019 22:33
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 dasgoll/e643290575006f6602a7a2ba474b61f9 to your computer and use it in GitHub Desktop.
Save dasgoll/e643290575006f6602a7a2ba474b61f9 to your computer and use it in GitHub Desktop.
wait bash built-in command
execute something/service in the background
and based on its exist status, execute something else
example
i have installed MySQL NDB cluster. It needs around 2-3 minutes to synchronise the database in between cluster nodes. Once the synchronisation finished only, I can start another application called heartbeat. Therefore in my shell script, I want to check the availability of the ndbd service all the times. Once this service become available, I need to start the heartbeat application
# send to background
./ndb_startup_script &
#
# wait on pid of ndb_startup_script
wait $!
if [ $? -eq 0 ] # assuming 0 is ret code for successful run
then
./heartbeat_startup_script
else
# extra stuff to handle error
fi
wget -q http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-1503.qcow2.xz
wait $!
=======
wait makes the shell wait for the given subprocess. e.g.:
workhard &
[1] 27408
workharder &
[2] 27409
wait %1 %2
delays the shell until both of the subprocesses have finished
====
The wait command in bash has a -n option:
If the -n option is supplied, wait waits for any job to terminate and returns its exit status.
This means that you may do
command1 &
command2 &
command3 &
wait -n
printf 'One command exited with exit code %d\n' "$?"
-
wget -q http://isoredirect.centos.org/altarch/7/isos/aarch64/CentOS-7-aarch64-NetInstall-1810.iso &
wget -q http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2.xz &
wait -n
printf 'One command exited with exit code %d\n' "$?"
===
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment