wait command in bash is a process management command that waits for the specified process running in the background to complete.
It returns the exit status of the command that is waited after the completion. The execution of the rest of the scripts will continue after the waited process is completed.
Table of Contents
Introduction to the wait Command
The syntax of the
wait command takes various forms. Let’s discuss each of them below.
The general syntax looks like this.
wait [option] ID
ID is the process ID or the job ID. This syntax can be used to wait for a job or process with a particular ID. In order to find the process ID of a particular command, you can use the following command.
[process] is the name of the process.
You can also use the
top command to see the information about all the processes that are running. You can find the PID of the process from it.
wait command can also be used without specifying an ID. In such a case, it waits for all the child background processes that are running.
Before demonstrating an example of the
wait command, there are a few other parameters that you need to know. They are:
&: The ampersand symbol used after a command indicates that the command is running in the background.
$!: The dollar symbol followed by an exclamation symbol gets the PID of the last run command.
$?: The dollar symbol followed by a question mark symbol gets the exit status of the last run command.
The example below shows how these parameters work.
sleep 15 &
 is the shell job ID, and
95292 is the process ID of the command
You can use the
$? parameters as shown as below.
echo $! echo $?
The first line of the output displays the process ID, while the second line shows the exit status that indicates the process was completed successfully.
Use the wait Command with PID
You have learned how to get the process ID of a command. Using that PID, you can also use the
wait command on that particular command.
Now, let’s look at the following demonstration of the
sleep 5 & pid=$! wait $pid echo "after 5 seconds"
In the example above, the first line of the script runs a background process for
5 seconds. The
sleep command is used to emulate that a certain background process takes
5 seconds to complete. In the second line, a variable
pid is created that stores the PID of the last run command, which is
sleep 5. Next, the
wait command is used in the third line, where the pid variable is the argument. As a result, the command waits for five seconds. After five seconds, the remaining script continues and prints the following.
after five seconds
Use the wait Command with the Job ID
In this section, you will learn how to use the Job ID with the
wait command. For example, write the following command.
sleep 10 &
It gives the following output.
Since we got the job ID of the process, we can use it with the wait command as follows.
Here, the value
8 is the job ID of the process. The above command halts the further execution by waiting till the running process completes. After ten seconds, the following output is shown.
+ Done sleep 10
Use the wait Command with the -n Option for Multiple Processes
In this section, you will learn how to use the
wait command with multiple processes. Using the
wait command with the
-n option only waits for the first process among the processes running in the background. Look at the following example for the demonstration.
echo "the starting time" echo $(date +%T) sleep 5 & sleep 3 & sleep 2 & wait -n echo "First job has been completed at " echo $(date +%T) wait echo "All jobs have been completed at " echo $(date +%T)
In the example above, the first thing done is to display the starting time of the script. Then, three background processes that last for five, three and two are run. Next, the command
wait -n is run. It only waits for the least time-consuming process.
sleep 2 is the least time-consuming process among the three. After it completes, the remaining part of the script starts to execute again.
After printing the two
echo commands, the
wait command is executed again. It has not taken any options and parameters. So, it means that it will wait for all the remaining processes.
After five seconds, the
echo commands after the
wait command in the script will execute.
the starting time 14:19:30 First job has been completed at 14:19:32 All jobs have been completed at 14:19:35
As shown in the output above, the
wait -n command waits for two seconds, which is the completion time for the first job. Next, the
wait commands wait for the rest of the background processes to complete, which is five seconds.
Use the wait Command with the -f Option
When using the
-f option with the
wait command, the particular process does not return the exit code when its status is changed. It actually terminates. Let’s see the following explanation.
At first, let’s take an example without using the
-f option. First, run the following command.
sleep 30 &
Next, use the
wait -f command with the PID of the above process as follows.
100366 is the PID. Next, open another terminal and kill the process with the following command.
kill -STOP 100366
As a result, the process gets killed, and the
wait command completes with an exit code.
+ Stopped sleep 30
Now, follow the same steps again, but this time, use the
wait -f command instead of
wait. In this scenario, the
wait command does not complete. The output looks like this.
wait -f 3295
+ Stopped sleep 30
Thus, in these various ways, you can use the
wait command in bash to wait for a process or job to complete before running the following script.