We have a wait command of Linux to wait for one or more than one processes to complete in the system. The versatile wait command of Linux allows us to find out when a specific process is completed. The termination status will be returned as a result, allowing us to identify if the process is successful in terminating or failed for some other reason. The wait command continues to run as it waits for specific background tasks to be completed. The Linux wait command is used in the use cases below to show how it functions.
Example # 1: Using a Wait Command for Bash Single Process.
We use the wait command to specify when a script’s background process must start processing. In the following bash script, we have used the echo command which prints the statement “Background Process” which means that the background process is processing. With this echo command, we have used the symbol “&” which is used to run a command in the background. Then, we have again employed the two echo commands which print the statement “First task” and “second task”.
After that, we invoked the wait which will pause to wait for the background process to finish after the second process before moving on to the third process. The third task will be processed at the termination of the background process which will be ended anytime.
echo Background process &
echo First task
echo Second task
wait
echo Third task
The output of the wait command displayed that after the completion of the first task and second tasks, the third task is processed. The wait command generates the statement “Background process” when the first and second tasks take time to complete.
First task
Second task
Background process
Third task
Example # 2: Using a Wait Command for Bash Multiple Processes.
As the earlier bash script uses the wait command for the single process, we are using the wait command in the bash script for multiple processes to be performed. When the wait command is used with the -n flag, it only waits for the initial process among the background processes. The bash script is provided below where we first deployed the three sleep commands set with specific values. The sleep command is defined with the sign “&” which we have already discussed earlier which indicates the background processes are running.
The first sleep command indicates that the background process needs “10” seconds to complete. The background process needs “5” seconds for completion and the third sleep command requires “3” seconds for the execution of the background process. Next, the wait -n command is called which only waits for the shortest process. Here, the shortest process takes three seconds to complete among the other processes. When the shortest process is completed then the echo statement “Completed the first job” will be printed.
After that, we have again deployed the wait -n command. This will again wait for the less time-consuming process. Now, the less time-consuming process takes five seconds as the process of time three seconds is executed. After waiting for five seconds of the process, the next echo statement “completed next job” will be displayed. The next command of wait does not use any flag which means that this wait command will wait for all the processes to be completed first and then it displays the echo statement “Completed all job”.
sleep 10 &
sleep 5 &
sleep 3 &
wait -n
echo “Completed first job.”
wait -n
echo “Completed next job.”
wait
echo “Completed all job.”
The results of the bash script of multiple processes with the wait command above are displayed on the following bash shell screen. The wait with the option -n commands pause for 3 and 5 seconds which is the amount of time it takes for the first and next job to finish the wait commands then pause for ten seconds which is the amount of time it takes for the remaining background processes to finish.
“Completed first job.”
“Completed next job.”
“Completed all job.”
Example # 3: Using a Wait Command for Bash Multiple Processes with Process ID.
When we are dealing with multiple processes in the bash script with the wait command then process id is helpful to identify the process. Here in the bash script, we have used the echo command to print the statement “Process1 for 3 seconds”. Then, we used the AND operator “&&” to set another command which is the sleep command. The sleep command takes a value of “3” which is the time for the background process to be completed. Then, we declared the variable “ProcessID” which provided a “$!” value. The symbol “$!” gets the process ID of the last executed command. The last executed command which takes “3” seconds is stored in the variable “ProcessID”.
Now, we have specified another command of echo which prints the “Process1 for 5 seconds” along with the sleep command. The sleep command takes five seconds for the completion of the second background process. Next, we printed the current time by using the DateTime structure of the bash. Then, the wait command is utilized where the variable “ProcessID” is set as an argument. The wait command now holds the wait time of the processID variable which is three seconds.
After three seconds, the remaining process of the bash script will be completed. Note that, we have another wait command which is passed with the sign “$!” as an argument Then, the last echo statement will be executed. for fetching the last run command process ID.
echo "Process1 for 3 second" && sleep 3 &
ProcessID=$!
echo "Process1 for 5 second" && sleep 5 &
echo "Current time $(date +%T)"
wait $ProcessID
echo "Process1 termination time $(date +%T) with exit status $?"
wait $!
echo "Process2 termination time $(date +%T) with exit status $?"
The output first displayed the completion of the process along with the time for completion. Then, the current time process is completed. After that, the wait occurred and then the next echo command was executed which displayed the exit status value zero along with the termination time. The value zero exit status represents that the process is successfully terminated. Then again, the pause occurs for the time the last process takes to complete and the last statement will be displayed.
Process1 for 5 second
Process1 for 3 second
Current time 00:31:20
Process1 termination time 00:31:23 with exit status 0
Process2 termination time 00:31:25 with exit status 0
Conclusion
The article provides a way to wait for a specific process to complete in Linux. We have used the wait command to wait for the running process. We first use the wait command for the single process to wait and then we have called the wait command for the multiple processes. Then, the wait command is called over the process ID to get the process ID of multiple processes.