Thursday, November 21, 2024
HomeA-Z CommandsWait Command in Linux Explained with Practical Examples

Wait Command in Linux Explained with Practical Examples

The wait command is something that comes with Linux so that you can find it in all Linux distributions. Before a process stops, it waits for it to finish. When you include a process or job ID, you should use the wait command. Simply put, wait commands containing an ID will continue to wait until the process finishes and returns to termination status. So if you also want to use the wait command, we recommend you to read this article thoroughly.

What is the Wait Command?


The wait command in Linux proves to be a valuable and easy process when we talk of managing the automation workflow. In addition, it helps you set up the flow correctly so that automation goes smoothly. For instance, using the wait command, you need to ensure that the current module finishes executing before the next one can start working.

This command monitors the previous process closely. A status will be sent to the command upon completing the previous process. Consequently, if you are waiting for a process with ID 25351 to complete, the wait command will submit an exit status when it completes. Its exit status will be found in its return message. Here is the general syntax of the wait command: 

wait [options] ID

ID(PID) –> The process ID (wait for PID) will wait until the utility is terminated if you choose this option. You can find the PID of a process through the following command: 

pidof <process name>

For example, we want to find the PID of Firefox, and then we will execute the below command in the terminal:

Wait Command PIDOF

Practical Examples of Wait Command in Linux


Below are some examples of the wait command that will help you better understand it. We have used different scenarios and created scripts accordingly, so let’s take a look at them:

1. Wait Command with Multiple Processes


Here in this example, we will determine the functioning of the wait command with numerous processes. We will draft a script where we can proceed with running two commands and then wait for them to execute. We have created a bash script “ubuntupit.sh” with the following information: 

#! /bin /bash

echo "wait vlc" &

process_id=$!

echo "wait firefox" &

wait $process_id

echo process 1 ended as $?

echo process 2 ended as $?

After creating the above script, we have executed the below command in the terminal:

bash ubuntupit.sh

Wait Command for Multiple Process

We can also use the wait command in the shell scripts, which are used for spawning the child process. So here is the simple script: 

#! /bin /bash

vlc &

process_id=$!

echo "PID: $process_id"

wait $process_id

echo "Exit Details: $?"

In the above command:

  • The first line consists of shebang, and the second line has vlc & for using wait command into it. 
  • $! works as an internal bash variable for storing the PID of the last process, which runs in the background.
  • PID is passed to the given wait command in the next line, which will wait until the vlc player closes.
  • In the end, the system will print the exit details of the wait command. 

Now, execute this script in the terminal:

Wait command for vlc script

We can also use the -n option in the script to get a different result. Here is a script “Ubuntupit2.sh” consisting of the -n option:

#! /bin /bash

vlc

firefox

wait -n

echo "First process ended"

wait

echo "All processes ended"

The -n option in the above script delays printing the echo statement until the first process is completed. The wait command waits until all background processes have finished.

Wait command for multiple procedures

2. Kill a Process Using Wait Command


With regards to the second example regarding the wait command, we will run a script named ubuntupit3.sh to terminate a process, and then we will run the wait command.

#! /bin/bash
echo "Kill a Process"
vlc &
process_id=$!
kill $process_id
wait $process_id
echo $process_id is ended successfully.

You will receive the output upon running the script:

Wait Command to Kill a Process

Here you can see that the exit status indicates whether the process has been terminated. This example shows how the exit status comes back differently based on the outcome of the process.

3. Checking the Exit Status Value


In this example, you can see that the function check () function requires two arguments; let’s name our script as ubuntupit4.sh. In this script, we are using the sleep command to demonstrate the example. We are defining the following function here:

#! /bin/bash

function check()

{

echo "Execute sleep for $1 seconds"

sleep $1

exit $2

}

check $1 $2 &

b=$!

echo "System is checking the status of the process"

wait $b && echo Ended Successfully || echo Not Ended Successfully

In this function, we first reflect the first process’s length. We then use the particular information to sleep and exit the process. We take into account the following input:

check $1 $2 &

b=$!

echo "System is checking the status of the process"

wait $b && echo Ended Successfully || echo Not Ended Successfully

After the user inputs a value, the wait command is used to output the status. Here we have used 6,5 and 6,0 as an example:

Wait command with exit status

Finally, Insights


The above article involves all the information about the wait command, its usage in Linux, and the working examples. You have seen how you can make good use of it in automation workflows. Everything mentioned in the article reads adequately, from the command used to its additional details, and does not miss any point.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now