How to Use Bash Wait Command in Linux

Learning how to use the bash wait command in Linux offers an effective way to make room for the process in the background while it changes its state. Here is how you can use the command and desirably wait for any process to complete. 

The Linux shell features a  great utility called the wait command that helps mentor the completion of any running process in the background. In simple words, the wait command is a versatile tool that plays a significant role in circumscribing the space when any desired process needs to be concluded.

Many of us often tend to complicate wait and sleep commands, while they’re pretty much different. The sleep command is generally used to delay any process for a particular duration. On the other hand, the wait command waits for the process to finish before returning to the termination status.

In this article, we’ll dive deep into how to use bash wait command n Linux and look at how it operates in bash scripts.

How to Use Bash Wait Command in Linux

When we talk about the wait command, there are usually several ways to use it in the bash scripts.

1. The general wait Command

It signifies that it waits for each background process to conclude, even before continuing the script. The command doesn’t even need any parameters, to begin with.

2. The wait -n Command

`It only waits for the background process (that follows right after) to complete and then returns an exit status.

3. wait <job ID> Command

Speaking of the wait <process or job ID> command, it makes an added job ID or PID wait for a specific process to conclude before eventually continuing the script.

4. The wait -f Command

It first waits for the background task to end, then terminates a program before quitting.

terminal window

Before working with the wait command in bash scripts, there are a few things that you’ll need to have access to. Firstly, the command line or Terminal with sudo (Administrator) privileges for running all the commands alongside a text editor. 

In addition, there are three vital parameters to be well-aware of. 

To begin with, we’ve “$!” for fetching the PID of the previous background processes. It usually stores the PID in any variable while operating multiple background processes. Then there is the ampersand symbol. In Linux, using “&” right after a command indicates any prevailing background job. And finally, the “$?” parameter is used to print the exit status of the last process.

background processes

To learn about the parameters better, launch the Terminal using the Ctrl+Alt+T shortcut and input the following command:

Here & signifies the background process, $! stores the PID of the background process, and $? makes room for the exit status. The value displaying 0 indicates that the command has finished successfully.

sleep 20 &
echo $!
echo $?

How to Use Single Process wait Command

Let us walk through a quick example to understand the usage of wait command for single processes.

single process wait command

1. The first thing that you’ll need to do is launch the Terminal.

2. After that, create a simple background process by inputting the following command:

sleep 20 &

3. Once done, confirm that the job is running in your background using the command:

jobs -1

4. Next, you’ll need to wait command but make sure not to define any parameters for pausing here.

pausing parameters

5. The Terminal will then wait for the background process to finish. As the input is sleep 20, the console will print a message called Done after 20 seconds.

How to Use Single Process bash wait Command

Here is an example that shows how to operate the wait command for single processes in bash scripts.

1. Firstly, launch any text editor and add the following code:

#!/bin/bash
echo Background process $
echo First work
echo Second work
echo Third work
wait 
echo Fourth work
 single process Bash Wait command

Here, the wait command right before the Fourth work will invoke a pause. In simple words, the system will wait for the other background processes to be complete before continuing with the fourth work. 

2. Now save the script, but make sure to name it as single_process.sh.

3. Next, launch the Terminal and update the permissions so that the saved script becomes executable.

4. Once done, run the command as:

sudo chmod +x single_process.sh
single process command

Finally, run the script with:

./single_process.sh

The background process will conclude at any point in time before the corresponding wait command, and the script will continue as usual.

Wait Command for Multiple Processes

Similar to the single process scenario, the following example will help you understand the working of the wait command for multiple processes.

1. The first thing to do is, of course getting yourself inside a text editor and input the following multiprocess script:

#!/bin/bash
sleep 20 &
sleep 25 &
sleep 15 &
echo $(date +%T)
wait 
echo $(date +%T)
multiprocess script

2. After that, save the script as test.sh.

3. The next thing to do is launching the Terminal and make the script executable. Run the following command:

sudo chmod +x test.sh
test script

Finally, run the script with

./test.sh

4. With that, all the three processes running in the background must be finished in 25 seconds.

This is how you can use the wait command in bash scripts for multiple processes. To understand the scenario better, move a step ahead and use the above-mentioned script in the following cases.

Adding the Job ID

To indicate the job for which the script should wait, you can add the job ID. Doing that is pretty simple. Use, wait %1 to pause the first process (sleep 20) to conclude.

Adding the -n Parameter

The -n parameter when added to <strong>wait</strong> completes the fastest process. In other words, the script should finish in fifteen seconds.

Bash Wait Command with PID for Multiple Processes 

Another efficient way of working with multiple processes is making use of PID. It helps in identifying the process and makes the usage of wait commands easier. 

1. At first, launch a text editor and add the following script:

#!/bin/bash 
echo "Process 1 lasts for 6s" && sleep 6 & 
PID=$! 
echo "Process 2 lasts for 12s" && sleep 12 & 
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 $?
bash wait command with PID

2. Once you’re done with that, save the script and name it as multi_wait.sh.

3. After that, you’ll need to make the script executable by inputting:

sudo chmod +x multi_wait.sh
multi-wait

4. Lastly, run the following script:

./multi_wait.sh

The script will take 6 seconds to complete the first process due to the availability for sleep 6, and the same for the second process is 12 seconds. 

This is pretty much everything about the usage of wait commands in bash scripts. With the help of detailed examples, we’ve tried to put in the best of efforts and make you learn about using wait commands for single and multiple processes.

Leave a Reply
Related Posts