How To Use Bash wait Command on Linux

In this guide, we want to teach you How To Use the Bash wait Command on Linux.

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.

The bash wait command is often used with the process IDs or Job IDs command.

Steps To Use Bash wait Command on Linux

In this guide, you will learn to use the bash wait command on your Linux system.

First, log in to your server as a root or a non-root user with sudo privileges.

Now you can follow the steps below to see how to use the bash wait command.

Bash Wait Command

The general syntax of the Wait command in Linux is:

wait [option] ID

ID would be a process ID or Job ID.

First, you need to know that there are useful parameters when working with the wait command in bash scripts.

bash script wait parameters

&: The ampersand sign (&) after a command indicates a background job.

$!: Fetches the PID of the last background process.

$?: prints the exit status of the last process.

Let’s see how these parameters work.

Open a terminal and run the following commands:

# sleep 10 &
# echo $! 
# echo $?
Output
# sleep 10 &
[1] 79496
# echo $!
79496
[1]+ Done sleep 10
# echo $?
0

The $! parameter stores the background process PID, while $? storing the exit status. The exit status 0 indicates the command finished successfully.

Create a Single Process with bash wait

At this point, we want to show you a single process with the wait command.

Create a simple background process with the following command:

sleep 20 &
Output
[1] 79513

Then, confirm the job is running in the background:

jobs -l
Output
[1]+ 79513 Running sleep 20 &

Next, use the wait command to pause until process completion:

wait

After 20 seconds, the console prints a Done message.

Output
[1]+ Done sleep 20

Now let’s see how to use the wait command in a bash script.

How to use the wait command in a bash script

First, you need to create and open a file with your favorite text editor, here we use vi:

vi BashWait.sh

Then, write a script in your file. For example, add the following script to your file:

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

When you are done save and close the file.

Next, you need to set the correct permissions for this file:

chmod +x BashWait.sh

Now run your script with the command below:

./BashWait.sh

In your output you will see:

Output
PID: 79648
Exit status: 0

The wait command has two useful parameters.

-n: Waits for only the following background process to complete and returns an exit status.

-f: Terminating a program first waits for the background task to finish before quitting.

For example, open the file that you have created in the previous step. And remove the script inside it and add the following script in the file.

vi BashWait.sh
#!/bin/bash
sleep 30 &
sleep 8 &
sleep 7 &
wait -n
echo "First job has been completed."
wait
echo "All jobs have been completed."

When you are done, save and close the file.

Then, run the script again and when the first job is completed, it will print the message on the terminal and wait for all other jobs to be completed.

./BashWait.sh

In your output you will see:

Output
First job has been completed.
All jobs have been completed.

Another example to use the wait command is to create two script files named hello and bash with the commands below:

vi bash.sh

Add the following script to the bash file:

#!/bin/bash
echo “Started bash.sh”
echo “Started hello.sh”
./hello.sh &
process_id=$!
wait $process_id
echo “Completed hello.sh

When you are done, save and close the file.

vi hello.sh

Add the following script to the hello file:

#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9 10
do
      echo “hello.sh – Loop number $i.”
done

When you are done save and close the file.

Then, set the correct permissions for the files:

# chmod +x bash.sh
# chmod +x hello.sh

Next, run the bash script with the command below:

./bash.sh

In your output you will see:

Output
“Started bash.sh”
“Started hello.sh”
“hello.sh – Loop number 1.”
“hello.sh – Loop number 2.”
“hello.sh – Loop number 3.”
“hello.sh – Loop number 4.”
“hello.sh – Loop number 5.”
“hello.sh – Loop number 6.”
“hello.sh – Loop number 7.”
“hello.sh – Loop number 8.”
“hello.sh – Loop number 9.”
“hello.sh – Loop number 10.”
“Completed hello.sh

Conclusion

At this point, you learn to Use the Bash Wait command on Linux.

Hope you enjoy it.

May this article about How To Configure Automatic Kernel Updates on Linux be useful for you.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

2 Comments

Leave a Reply

Stay informed and not overwhelmed, subscribe now!