Scripting is an essential skill in today’s environment where everything involves computers. But the skill in demand is efficient scripting. In Bash, there are multiple commands that can make your scripts magical, and the wait command is one of them. We encourage you to go through our other articles as well where we explain every single bash command in detail. This article is not an exception as we will guide you through the wait command in bash. We will start with what the wait command is capable of, how it works, then some examples, and the best practices to use wait command in bash.
What is the bash wait Command?
The reason why many seasoned professionals struggle to write concise scripts is they do not know all the uses of a particular utility. To eliminate this, we at Unixmen always go from the basics and in this article as well, we are going to learn right from the beginning: what does the bash wait command do?
The wait command in bash scripting is a built-in utility to make Shell wait for a running process to complete. Consider a scenario where you are executing a command or script. There will be too many processes that run in a sequence or simultaneously. These processes often run in the background. In many cases you may face errors because one process should have been run after the completion of a dependent process. If this sequence breaks, you may face errors, and it takes too much time from you to debug.
How can you make a script pause until a specific process or all background processes are completed? With the bash wait command.
Here is the basic syntax for the bash wait command:
wait [processID or pid]
The process ID or the pid is the ID of the background job you are waiting for. But what if you want to wait until all the background jobs are complete? Simply do not specify a process ID and the bash wait command will pause until all background jobs are complete.
How does the bash wait command work?
Once you execute a command in the background using the ampersand param, Shell does not wait for the command to finish before it moves on to execute the next one. In these scenarios, you have to add the bash wait command to pause the execution of this script until the background process finishes. Let us take a step-by-step approach to learn how the bash wait command works.
How to use Bash Wait Until a Command Completes
After running a command in the background, you can use the “wait” command to pause the script until the process finishes. To do this, simply execute the command
wait
When this command is executed, execution of the script will wait until all the background processes are completed. But what if you would want to wait for one particular process? You can do that as well by specifying the process ID in the wait command. For example, if you would want to wait for process with PID 123 to complete, execute the command:
wait 123
Additional Benefit: Check Exit Status
The bash wait command gives you the option to view the exit status of the process it was waiting for. This helps you in finding if the process completed successfully. If we take the above scenario, execute the command:
wait $123 echo $?
This command displays the exit status of the process bearing PID 123.
Some Practical Use Cases for bash wait Command
Let us look into some use cases where the wait command can make your scripts more efficient and useful.
How to Run Multiple Commands Simultaneously
Consider a scenario where you have many tasks to perform, and they consume too much time. They can be anything like tar balling directories or scrape data from a site. When they are done in sequence, it can be performed simultaneously to save time. The bash wait command can be used to ensure all tasks are completed before proceeding.
sleep 111 & process_id_1=$! sleep 222 & process_id_2=$! sleep 333 & process_id_3=$! wait $process_id_1 wait $process_id_2 wait $process_id_3 echo "All tasks completed."
When this script is executed, three instances of sleep command are run in the background. The wait command ensures all processes are finished before moving to the next step.
How to Handle Error Conditions with bash wait
When you run multiple background tasks, failing to capture the exit status of the processes will pose error conditions. This simple failsafe will save you hours in debugging what went wrong. Here is a sample use case.
sleep 111 & process_id=$! wait $process_id status=$? if [ $status -eq 0 ]; then echo "Process completed successfully." else echo "Process failed with status $status."
Let’s see what happened in this script. Initially we assigned a task i.e., to put process bearing pid 111 to sleep. Then we used the bash wait command to wait until the process was complete and fetched the status of the process. Then we used the response from the command to display the success or failure message.
Wrapping Up
Let us wrap up with some best practices for using the bash wait command.
- Always capture the PIDs using $! And pass them to wait.
- Always check the exit status of the background processes after using the bash wait command. This helps you in handling the errors appropriately and perform troubleshooting conditions if required.
- Do not over do background jobs. When you run tasks in parallel, though it can improve performance, it also overloads the system’s compute capacity with many concurrent jobs. Keep an eye on your system resources when you run many background processes.
Related Link
Ubuntu’s documentation on wait command
Some Articles You Would Love