Bash wait Command with Examples

September 23, 2021

Introduction

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.

This tutorial explains the wait command syntax and provides example bash script codes.

Bash wait Command with Examples

Prerequisites

  • Access to the command line/terminal.
  • Administrator (sudo) privileges.
  • A text editor, such as Vim or Nano.

Note: You might also find useful our guide on how to run a bash script that features several different techniques.

bash wait Command Syntax

There are different ways to use the wait command in bash scripts. The table below explains each use case.

Command Explanation
waitWithout any parameters, the wait command waits for all background processes to finish before continuing the script.
wait <process or job ID>An added PID or job ID waits for a specific process to end before continuing the script.
wait -nWaits for only the following background process to complete and returns an exit status.
wait -fTerminating 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.

2. $! fetches the PID of the last background process. Store the previous PID in a variable when working with multiple background processes.

3. $? 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 $?
Terminal output of the PID and exit status of a background process

The $! 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:

jobs -l

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:

wait
Terminal output of wait command

The terminal waits for the background process to finish.

Terminal output of wait command done process

After 10 seconds (due to sleep 10), the console prints a Done message.

Single Process bash wait Example

Use the 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
Single process wait bash script example

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:

./single_process.sh
Output of single process script

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)
Multiple processes wait bash script

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:

./test.sh
Output of multiple processes bash script

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 -n parameter to <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 %1 pauses 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 $?"
Multiple processes wait bash script with PIDs

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:

./multi_wait.sh
Output of multiple processes wait bash script with PIDs

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.

Conclusion

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. Next, use our Bash Function guide to reduce repetitive code and speed up your scripting.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
How to Use the Linux sleep Command with Examples
February 3, 2021

The sleep command is used when it is necessary to postpone the execution of commands on the command line or in shell scripts. This tutorial lists ways in...
Read more
How to Use the Linux tee Command
January 28, 2021

By default in Linux, a command received through standard input writes directly to standard output. The tee command changes that behavior by allowing another command to use a file as the output. This tutorial lists ways in which...
Read more
Linux Commands Cheat Sheet: With Examples
February 21, 2020

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or download our Linux Commands Cheat Sheet and save it for future reference.
Read more
How to Use the Linux xargs Command
February 11, 2021

While some Linux commands take input both from the standard input (stdin) and as a command-line argument, some are limited to take input only as an argument. To process input from stdin, those commands need to utilize the xargs...
Read more