X

What is the Wait Command in Linux? (With Examples)

Spread the love

In this tutorial, we will explain what the wait command is, as well as providing a few usage examples so that you can better understand how to use it on a Linux VPS.

wait is a versatile tool which you can use in Linux to determine when a given process or job is completed. It will return its termination status as a result, with which you can understand whether the process completed successfully or if it failed for some reason. While this command looks similar to the sleep command, they fulfill different purposes. Let’s get into it.

wait is different from sleep. Many users which are new in Linux find it difficult to understand the difference between these two. In general, these two commands are quite different and are used for different types of tasks. As we mentioned earlier, the wait command waits for a process to finish and it returns the termination status. On the other hand, sleep is typically used to delay a certain job for a specified amount of time. You will often need to use these two commands in your custom bash shell scripts, so it is important to understand the difference. Let’s get into that.

To test how the wait command works, you will need to connect to your Linux server via SSH. Log in to your server and start a simple process in the background. Run the following command in your terminal to do so:

sleep 60 &

The command above will start a process in the background. That process will run for 60 seconds, after which it will be terminated. The output of the command will be the PID of the process, so you will see something like this on your screen:

# sleep 60 &
[4] 4455

In this case 4455 is the PID of the process. In order to find the PID of a different process, you can use the following command:

ps axuwf | grep "sleep 60" | grep -v grep | awk {'print $2'}

where "sleep 60" is the name of the command/process. In our example, it will return the following:

# ps axuwf | grep "sleep 60" | grep -v grep | awk {'print $2'}
4455

Now that you know the PID of the process, you can run wait and wait for the process to complete in order to see the termination status.

wait 4455

Once the process finishes, you will see the following in your terminal:

# wait 4455
[3]   Done                    sleep 60

The output tells you that the sleep 60 process is done and no longer running on your Linux VPS.

As you can see in the example above, you need to pass the process ID as the argument to wait. Basically, wait will return the termination status for any and all specified processes. You can specify multiple processes at once by providing the process ID or a job specification. If you provide a job specification, all processes in that job’s pipeline are waited for and considered.

The wait command comes in handy when you need to run a specific command after some or all of the processes are finished. You will find this useful when writing shell scripts or other programs. The example below shows you how to use wait in a shell script.

Create a new shell script in the /opt directory:

cd /opt
nano wait.sh

You can use a text editor of your choice, we are using nano in our example. Paste the following lines in the file:

#!/bin/bash
echo "Process 1 is running for 5 seconds ..." && sleep 5 &
PID=$!
echo "Process 2 is running for 15 seconds ..." && sleep 15 &
wait $PID
echo "Process 1 exited with status $?"
wait $!
echo "Process 2 exited with status $?"

Save the file and close it. Once you close the file, make it executable using the following command:

chmod a+x wait.sh

To run the shell script that you just created, use the following command:

./wait.sh

Once you run the shell script, you will see the following output in your terminal:

Process 1 is running for 5 seconds ...
Process 2 is running for 15 seconds ...
Process 1 exited with status 0
Process 2 exited with status 0

As you can see in the example, both processes are started and running for the specified number of seconds. Once they are finished, the output with their termination status will be echoed on the screen. Of course, you can modify the script and try different tasks to better learn how the wait command works. Hopefully, you will find it useful.


If you are one of our Managed Linux Hosting Support customers, you can always ask our expert Linux system admins to help you with any aspect of using or configuring your server. They are available 24/7 and can take care of your request immediately.

PS. If you liked this post on what the Linux wait command is, please share it with your friends on the social networks using the share shortcuts, or simply leave a reply below. Thanks.

Categories: Tutorials
admin:
Related Post