Sep 1, 2023 4 min read

How to Use Bash Wait Command on Linux

Use Bash Wait Command on Linux with our step-by-step tutorial. Wait command waits for command to complete & returns exit status.

Use Bash Wait Command on Linux
Table of Contents

Before, we begin talking about how to use bash wait command on Linux, let's briefly understand - What is wait command?

This command waits for the given command to complete and returns the exit status as waited for the command.

In most shells, the wait command is present as a built-in command because it affects the current shell execution environment.

In this tutorial, you will use the built-in wait command. We will also address a few FAQs on how to use bash wait command on Linux.

Bash wait Command

Below is the syntax of wait command:

wait [options] ID

The ID is the process ID or job ID. When no ID is provided, this command waits until the child background jobs are all completed.

When the wait command is executed, it returns the status of the last command waited for.

The following code would be used to wait for a background process with PID 7124:

wait 7124

The command waits until all processes have been completed when multiple processes are specified.

Defining a job is done using the job specification ("jobspec"), which identifies the processes associated with a job. There are two parts to a jobspec: the percentage symbol (%n) and the job number (%L). Below is an example:

rsync -a /home /tmp/home &

On your terminal, you will see the shell job ID (surrounded with brackets) and process ID:

Output

[2] 51299

Run this command followed by the job specification to wait for the job:

wait %2

The -n option instructs the command to wait only for one job from the given pids or jobspecs to complete and return its exit status. In the absence of arguments, wait -n awaits the completion of any background jobs and return the status of the existing jobs.

wait -n 15629 65239 19746

In the example above, wait -n prints the return status of the first-to-exit job, but does not show the job's PID. In order to get the job specification or job pid for which the exit status is returned, use the -p option:

wait -p job_id -n 15629 65239 19746

As of Bash 5.1, the -p option is present. If you’re using an older Bash version, you’ll receive an error message indicating that the option is invalid.

The -f option instructs wait to return its exit code only when pids or jobspecs actually terminate, rather than when the status changes. The -f option is only available with job control enabled. Interaction prompts are the only job types for which job control is enabled.

Examples

wait is commonly used in shell scripts that generate parallel-running child processes.

Create the following script to demonstrate how the command works:

#!/bin/bash
sleep 30 &
process_id=$!
echo "PID: $process_id"
wait $process_id
echo "Exit status: $?"

Let's go over the code one line at a time:

  1. Shebang is the first line, which notifies the operating system which interpreter to use to parse the rest of the file.
  2. We're simulating a time-consuming background process with the sleep command.
  3. $! is a Bash variable that stores the PID of the most recent background job performed. That is the PID of the sleep command in this example. The PID is saved in a variable (process id).
  4. The PID number is printed.
  5. The PID is supplied to the wait command, which waits for the sleep command to complete before continuing.
  6. The wait command's exit status is printed. $? is an internal Bash variable that stores the last command's exit status.

If you run the script, you'll get the following output:

Output

PID: 36353
Exit status: 0

Here's an example of how to use the -n option:

#!/bin/bash
sleep 3 &
sleep 30 &
sleep 5 &
wait -n
echo "First job completed."
wait
echo "All jobs completed."

When the script is run, three background processes are started. wait -n waits until the first job is finished, and the echo statement is printed. wait waits for all of the child's background jobs to finish.

Output

first job completed
all jobs completed

The -f option is explained in the last example. Run the following commands in the terminal:

sleep 3600 &
Output

[1] 46671

Wait for the procedure to complete:

wait 46671

Open a new terminal and use the kill command to terminate the process:

kill -STOP 46671

The wait command will complete and deliver the process exit code after the process status has been altered.


Follow the same procedures as before, except this time use wait -f $pid:

sleep 3600 &
wait -f 46671

From the other terminal, halt the process:

kill -STOP 46671

The wait command will not complete this time. It will continue to run till the sleep procedure is completed.

FAQs to Use Bash Wait Command on Linux

How do I use the wait command in a script? 

Just use the wait command followed by the process IDs (PIDs) of the background processes you want to wait for.

Can I use the wait command to wait for multiple processes simultaneously? 

Yes, you can provide multiple PIDs to the wait command, and it will wait for all of them to complete.

Is it possible to customize the behavior of the wait command? 

The wait command does not have any customization options; it simply waits for the specified processes to finish.

What happens if the processes I'm waiting for are already finished? 

If the specified processes have already terminated, the wait command will return immediately with their exit statuses.

Can I capture the exit statuses of the processes I'm waiting for? 

Yes, the wait command returns the exit statuses of the background processes, which you can store in a variable for further processing.

Is there a time limit for how long the wait command will wait? 

No, the wait command will wait indefinitely until all the specified processes are complete.

Can I use the wait command for background jobs within interactive shell sessions? 

Yes, the wait command is also helpful in shell sessions to wait for background jobs before proceeding.

Conclusion

We hope this detailed guide helped you understand how to use wait command on Linux.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.