This short post will explain how to use the bash wait command to wait until a process has finished, and how if differs to the sleep command.
What does the Bash wait Command Do?
The wait command is a simple program that waits for a process to change state – this means waiting for it to exit.
How to Use the Bash wait Command
The wait command expects a list of process ids that it should wait to complete, for example:
wait 111 222
Above, the process ids 111 and 222 are supplied to wait. wait will only complete its own execution once the processes supplied to it have exited. This will pause any script that calls the wait command at that point until the specified processes have finished.
How is the wait Command Different to the sleep Command?
The wait command is often mistaken for, used accidentally instead of, the sleep command. The sleep command pauses execution for a specified time, so it’s best not to get them confused!
Bash wait Example – Single Process
In the below example, the wait command is used to pause the script until a single process has completed:
#!/bin/bash # Start a background process process1 & # Wait for process to finish wait %1 # Continue script here...
Note that process1 can be a call to any program, so, for example, you could use echo to test:
#!/bin/bash # Start a background process echo "hello" & # Wait for process to finish wait %1 # Continue script here...
The ampersand (&) symbol is used to tell the command to run in the background – otherwise the script will wait for it to complete anyway.
Bash wait Example – Multiple Processes
Waiting for a single process that you’ve called isn’t all that useful, but when you have multiple processes, it means that they can finish in any order while your script continues to run:
#!/bin/bash # Start multiple background processes process1 & process2 & # Wait for processes to finish wait %1 %2 # Continue script here...
If we use this example with the echo command and the sleep command, we can see how this can be used:
#!/bin/bash # Start two background processes sleep 5 && echo "Hello 1" & echo "Hello 2" & # Wait for both processes to finish and get their exit status wait %1 process1_status=$? wait %2 process2_status=$? # Check if either process failed if [ $process1_status -ne 0 ] || [ $process2_status -ne 0 ] then echo "Error: One or more processes failed" exit 1 fi
Above, the sleep command delays the execution of the first echo command (the double ampersand (&&) runs the two commands sequentially, not to be confused with the single ampersand executes the command in the background). As they are instructed to run in the background, the script continues, executing the second echo command first. The script then waits for both commands to complete.
The above example also checks whether one of the processes failed. Once you have your shell scripts performing their intended tasks, it is good practice to expand them to handle common errors like bad user input or a failed process so that there are no unexpected results.