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.
This tutorial explains the
wait command syntax and provides example bash script codes.
- Access to the command line/terminal.
- Administrator (sudo) privileges.
- A text editor, such as Vim or Nano.
bash wait Command Syntax
There are different ways to use the wait command in bash scripts. The table below explains each use case.
|Without any parameters, the |
|An added PID or job ID waits for a specific process to end before continuing the script.|
|Waits for only the following background process to complete and returns an exit status.|
|Terminating a program first waits for the background task to finish before quitting.|
Wait Command Examples
There are three additional parameters to know when working with wait in bash scripts:
1. The ampersand sign (
&) after a command indicates a background job.
$! fetches the PID of the last background process. Store the previous PID in a variable when working with multiple background processes.
$? prints the exit status of the last process.
To see how these three parameters work together, open a terminal window and run:
sleep 10 & echo $! echo $?
$! parameter stores the background process PID, while
$? stores the exit status. The exit status
0 indicates the command finished successfully.
Single Process wait Example
1. Start by opening the terminal and create a simple background process:
sleep 10 &
2. Confirm the job is running in the background with:
Note: If the job shows as complete, try to change the sleep time to more than 10 seconds.
3. Use the
wait command without any parameters to pause until process completion:
The terminal waits for the background process to finish.
After 10 seconds (due to
sleep 10), the console prints a Done message.
Single Process bash wait Example
wait command to indicate by what point a background process must execute inside a script.
1. For example, add the following code in a text editor:
#!/bin/bash echo Background process & echo First message echo Second message wait echo Third message
If the background process does not finish the first and second process, the
wait command invokes a pause to wait for the background process to complete after the second process before continuing to the third process.
2. Save the script as single_process.sh. In the terminal, change permissions to make the script executable:
sudo chmod +x single_process.sh
3. Run the script with:
The background process completes any time before the
wait command, and the script continues.
Multiple Processes wait Example
1. Open the text editor and add the following script with multiple processes:
#!/bin/bash sleep 10 & sleep 15 & sleep 5 & echo $(date +%T) wait echo $(date +%T)
The script prints the current time before and after the
wait command. Without any parameters, the program waits for all processes to finish.
2. Save the script as test.sh and close the file. Next, make the script executable:
sudo chmod +x test.sh
3. Lastly, run the program with:
Since the processes run in the background, all three are completed in fifteen seconds.
4. Use the same script to test the following use cases:
- Add the
<strong>wait</strong>. Only the fastest process completes, and the script ends in ten seconds.
- Add the job ID to indicate for which job the script should wait. For example,
wait %1pauses for process 1 (
sleep 10) to complete.
Multiple Processes bash wait With PID Example
1. When working with multiple processes, use the PID to identify a process. The example script below displays a single use case:
#!/bin/bash echo "Process 1 lasts for 2s" && sleep 2 & PID=$! echo "Process 2 lasts for 3s" && sleep 3 & echo "Current time $(date +%T)" wait $PID echo "Process 1 ended at time $(date +%T) with exit status $?" wait $! echo "Process 2 ended at time $(date +%T) with exit status $?"
2. Save the script as multi_wait.sh. Make the script executable with:
sudo chmod +x multi_wait.sh
3. Run the script to see the output:
The script takes two seconds to complete the first process (due to
sleep 2) and three seconds to complete the second process. The two processes are executed simultaneously and both complete in three seconds.
Hopefully, the examples helped you learn how to use the
wait command in bash scripts and what output to expect when working with single and multiple background processes.