How to Wait For a Specific Process to Complete In Linux
If you are operating in a shell script of Linux and want to have the script’s desired outcome, it gets obligatory for you to use the wait command in your script. The wait command is implemented as a built-in command in most Linux distributions, which allows waiting for a specific process to complete, then returns its executed status. For its working, you need to use it with a parent id or a child id. In today’s article, we will discuss the approaches to wait for a specific process in Linux.
Note: We are going to use Ubuntu 20.04 for performing the specific process and its wait command.
How The Wait Command Works:
The command will monitor the last process. In the shell script, the first id number is given to the last statement of the script, and it is processed at priority. You can use the wait command to make it wait for the last statement and process the other statement firstly.
Step # 1: Create the File on Text Editor to write a Script:
Before performing the wait command, you initially have to create a script file in the text editor using the following appended command.
The “gedit” utility will directly open the text editor to write your script to execute. It will take some time to open, after which you will be able to use it immediately. The script editor will appear like the image given below:
Step # 2: Write a Script With Wait Command In Text Editor:
For using the wait command, initially, you have to write a script to apply the command in it. For writing a script in bash profile, always start it with #@/bin/bash as shown below:
In the below-given image, there is a script in which there are two jobs, one is assigned to the parent, and the other is assigned to the child. In line 3 there is a wait command that will allow the child to wait until the parent completes its process.
Save this above image script with any name using format .sh (.Sh) as shown in the given example.
Step # 3: Execute the process to check the Outcome of Script:
To execute the script, you have to go back to the terminal of the Linux server. For executing, you need to write the command of “bash” and the name of the created file saved in Linux. Here is the command appended below:
As soon as the execution of this command is completed, you will get the following output displayed in the snapshot below.
As shown above, the parent class executes firstly, and at that time child class is in the halt stage for its execution. Child class executes right after the execution of parents class. In this code, the Child has been terminated. “Child wait command 1” will be shown after the parent waits for command 2; this happens just because of the wait command given to the child class.
Another Example of Wait Command:
Knowing more patently about how this command works, you may have another example with different script execution. Repeat the same process to write a script as above mentioned by using the command “gedit” to open a text editor and after writing your script, save the file with any name mentioning .sh i-e new.sh as shown in the below-affixed image.
The output of this script will be diverse; there will be no sequence. It is up to the CPU to execute which function firstly, usually the CPU shows the last id at first; below shown is the output of this program:
In the above-affixed image, you can see the last id of the variable executed firstly, and this sequence follows till the first id.
Add the wait Command to get the proper output:
Now you will have to add the wait command to execute in an appropriate order. In the below image at lines 3,6,7, the wait command is added; now, it will allow the parent class to wait until the child class terminates.
To get the output of this program, perform the same method as shown above. Use the command with bash keyword along with the name of the already created code file. The output of this program is inaccurate in order and not assorted as it is shown in the appended image.
Here is the appropriate output of the program, and it can easily be understood. “Good to see you here” will be shown at the last because the wait command is applied at this statement, so this has to wait until other statements are processed and show the relevant outcome.
Some Specific Uses of the “Wait” Command:
Above, we have seen ample examples using the wait command to have a suitable and anticipated output of the script. When different processes are running in the shell, only the last command process id is present in the shell. So, for running the first command, firstly, you have to apply the wait command at your last. We use the wait command mostly in the program having multiple processes to have appropriate output.
Conclusion:
This article explained the detail about the wait command for some specific processes to complete in Linux. Above there are examples to understand the usage of the wait command and how it works in Linux. All you have to do is get benefit from this article and use the wait command in your script to have your own desired output.