I have a script which updates a web application. The web application is spread across 2 servers. Here is a rundown of the script
Each of the 7 steps are done one after the other synchronously. The total run time is approximately 9 seconds. To reduce downtime however, many of these steps could be done asynchronously.
For example, step 4 and 5 could be done at the same time. I want to start step 4 and 5 asynchronously (e.g. running in the background), but I cannot find how to wait until they are both completed before going further.
The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.
Bash isn't really asynchronous in the same way that JavaScript is asynchronous, however it can produce a result that would be similar to an asynchronous command in another language by forking.
A shell allows execution of GNU commands, both synchronously and asynchronously. The shell waits for synchronous commands to complete before accepting more input; asynchronous commands continue to execute in parallel with the shell while it reads and executes additional commands.
/bin/sleep is Linux or Unix command to delay for a specified amount of time. You can suspend the calling shell script for a specified time. For example, pause for 10 seconds or stop execution for 2 mintues. In other words, the sleep command pauses the execution on the next shell command for a given time.
You might want to use command grouping to maintain which steps need to be synchronous:
step1
( step2 && step4 && step6 ) &
( step3 && step5 && step7 ) &
wait && echo "all done"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With