Last active
February 8, 2019 22:33
-
-
Save dasgoll/e643290575006f6602a7a2ba474b61f9 to your computer and use it in GitHub Desktop.
wait bash built-in command
This file contains 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
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 $! |
This file contains 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
======= | |
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