In the world of shell scripting, process management and synchronization are critical. The Bash wait
command is a quick and easy way to coordinate and regulate the execution flow of several processes. Understanding how to use the wait
command correctly can improve the efficiency and dependability of your shell scripts greatly. In this article, we will deconstruct the Bash wait command, looking at its features, typical use cases, and best practices in Linux.
Table of Contents
Prerequisites
- Access to the command line/terminal.
- Administrator (
sudo
) privileges. - A text editor, such as Vim or Nano.
Syntax for Bash wait command
Syntax | Description |
---|---|
wait | Waits for a child process to terminate. |
wait [-n] job_id | Waits for the job with the specified job_id to terminate. The -n option causes wait to return immediately if the job is already terminated. |
wait -n | Waits for the next background job to terminate. |
Wait command
When working with wait
in bash scripts, there are three more parameters to be aware of:
- The ampersand sign (&) following a command denotes a background job.
- $! returns the
PID
of the most recent background process. When working with several background processes, remember to save the previousPID
in a variable. - $? displays the last process’s exit status.
To show how these three factors interact, launch a terminal window and type:
sleep 10 &
echo $!
echo $?
cloudbooklet@cloudbooklet:~$ sleep 10 &
[1] 15680 -
cloudbooklet@cloudbooklet:~$ echo 1
15680
cloudbooklet@cloudbooklet:~$ echo
0
cloudbooklet@cloudbooklet:~$
The $! argument holds the background process PID
, while $? stores the exit status. The exit status 0 indicates that the command was successful.
Single Process Wait
Begin by opening the terminal and running the following background process:
sleep 10 &
Confirm that the job is executing in the background by typing:
jobs -l
Note: If the job is marked as finished, consider increasing the sleep time to more than 10 seconds.
Use the wait
command without any parameters to pause until the procedure is finished:
wait
cloudbooklet@cloudbooklet:~$ sleep 10 &
[1] 49866
cloudbooklet@cloudbooklet:~$ jobs -l
[1]+ 49866 Running sleep 10 &
cloudbooklet@cloudbooklet:~$ wait
The terminal is waiting for the background process to complete.
cloudbooklet@cloudbooklet:~$ sleep 10 &
[1] 49866
cloudbooklet@cloudbooklet:~$ jobs -l
[1]+ 49866 Running sleep 10 &
cloudbooklet@cloudbooklet:~$ wait
[1]+ Done sleep 10 &
The terminal prints the Done message after 10 seconds (due to sleep 10).
Single Process Bash wait
Use the wait
command to specify when a background process within a script must execute.
- For example, in a text editor, enter the following code:
#!/bin/bash
echo Background process &
echo First message
echo Second message
wait
echo Third message
~
~
If the background process does not complete the first and second processes, the wait
command causes a pause to wait
for the background process to finish the second process before proceeding to the third process.
- Save the script as
single_process.sh
. Change the permissions in the terminal to make the script executable:
sudo chmod +x single_process.sh
- Use the following commands to run the script:
./single_process.sh
cloudbooklet@cloudbooklet:~$ ./single_process.sh
First process
second process
Background process
Third process
cloudbooklet@cloudbooklet:~$
The background process finishes once the wait
instruction is executed, and the script proceeds.
Multiple Processes Bash Wait
- Open a text editor and paste the following script with several processes:
#!/bin/bash
sleep 10 &
sleep 15 &
sleep 5 &
echo $(date +%T)
wait
echo $(date +%T)
~
~
The script prints the current time before and after the wait
command. Without any parameters, the application just waits for all processes to complete.
- Close the file and save the script as
test.sh
. Make the script executable next:
sudo chmod +x test.sh
- Finally, execute the program with:
./test.sh
cloudbooklet@cloudbooklet:~$ ./test.sh
14:32:22
14:32:37
cloudbooklet@cloudbooklet:~$
Because the operations are running in the background, all three are finished in fifteen seconds.
- Run the same script to test the following use cases:
- Add the
-n
argument towait
. Only the fastest process completes, and the script stops in ten seconds. - Add the job ID to specify which job the script should wait for. Wait%1 pauses for process 1 (sleep 10) to complete.
Multiple Processes Bash Wait With PID
- When working with many processes, utilize the
PID
to identify a process. The following example script illustrates 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 $?"
~
~
- The script should be saved as
multi_wait.sh
. Make the script executable by typing:
sudo chmod +x multi_wait.sh
- Run the script to see the results:
./multi_wait.sh
cloudbooklet@cloudbooklet:~$ ./multi_process.sh
Process 1 lasts for 2s
Process 2 lasts for 3s
Current time 15:56:22
Process 1 ended at time 15:56:24 with exit status ©
Process 2 ended at time 15:56:25 with exit status ©
cloudbooklet@cloudbooklet:~$
The script completes the first process in two seconds (due to sleep 2) and the second process in three seconds. Both operations run concurrently and take three seconds to complete.
Also read: You might also find useful our guide on How to Change Directory in Linux Using cd Command.
Conclusion
Finally, the Bash wait
command proved to be an indispensable tool for managing process synchronization in shell scripts. It ensures orderly execution and reduces errors by halting execution until defined procedures are completed. Incorporating the wait
command into your scripts enables you to enhance efficiency and effectively control concurrent processes. Please feel free to share your thoughts and feedback in the comment section below.