Home Learn Linux Wait command in Linux explained with examples

Wait command in Linux explained with examples

by Divya Kiran Kumar
Wait command linux

The wait command comes built-in with Linux; hence you can find it in any of the Linux distros. It is used to wait before a running process is completed. To make it work, you need to use it with a job id or a process id.

In a nutshell, the wait command with a given job ID or process ID will wait for the process to complete and return their termination status.

Wait command is handy when it comes to managing an automation workflow. It can help you to set the flow correctly so that the automation is successful. For instance, your automation process requires a particular module to finish execution before the next module can take effect. The wait command lets you do it.

Working of the wait command

The command will monitor the previous process. Once the previous process returns a status, it will then send an exit status. So, if you are waiting for a process with ID 25351 to complete, the wait command will wait for its completion before submitting the exit status. The return message will contain the process exit status.

General Syntax:

wait [Processid] [jobid]
  • Process ID –> If you use process ID (wait PID), then it will wait for the utility termination
  • Job ID –> In this case, it will wait for the background process that you have selected. It only works in the current shell execution environment.

The exit command is set to exceed a value of greater than 128 if the process terminates abruptly. So, if you see any amount higher than 128, this means that the process didn’t work as intended.

The success exit status is set to 0. It only happens if the process successfully terminates with no child process running in the current shell. However, if you do not receive 0 as exit status, then it means that there is some error. It is notified with the value anywhere from 1 to 126. Also, if you get an exit status of 127, that means the process id is unknown.

Practical Examples

To get a better understanding, let’s go through some of the practical examples below.

Wait command with multiple processes

In this example, we will see how the wait command works with various processes. We will create a script where we run two commands and then wait for them.

We name the script multiple.sh. Below is the code for it.

#!/bin/bash
echo "testing the wait command instance 1" &
process_id=$!
echo "testing the wait command instance 2" &
wait $process_id
echo Job 1 is completed and exited with status $?
echo Job 2 is completed and existed with status $?
wait-command-multiple

Testing wait command with multiple processes

The above is also an example of a script with the wait command. Also, it returns the status. To get a better understanding, let’s go through the script line by line. In the first line, we import the bash that we are going to use to run the script.

Next, we will echo to the shell that we are testing the wait command. We store the process id of the first process in the $process_id variable. Now, we wait for the first process to get executed. Once it is done, then it will output that the first process is completed and also output the status with the “$?”

Also, you can see that the second wait command is used with “$!”. This means that we use the second process id with the wait command.

In the end, both the processes are executed entirely and returned an exit status of 0.

Killing a process and using wait command

In the second example, we will kill a process and then use the wait command. Let’s name our script, killandwait.sh.

#! /bin/bash
echo "Killing process and using wait command"
sleep 10 &
procc_id=$!
kill $procc_id
wait $procc_id
echo $procc_id is terminate

If you run the above script, you will get the following output:

kill-and-wait

We kill a process and then use wait command on it

As you can see, that exit status is different and reads as the process is terminated. This example shows you how the command returns different exit status depending on the fate of the process.

Checking the exit status value

In the last example, we will go through a script that uses the check() function. The check() function takes two arguments. Let’s call our script waitandcheck.sh.

Here we define a function:

function check()
{
echo "Let's sleep for $1 seconds"
sleep $1
exit $2
}

As you can, this function first echos on how long the first process will sleep and then use those to sleep and exit the process. We take input using the lines below:

check $1 $2 &
b=$!
echo "checking the status"
wait $b && echo FINE || echo NOT FINE

In the above lines, we take the input from the user and then output the status using the wait command.

wait-and-check

Running the script with two inputs, 10 and 3

ok-status-wait-and-check

Running the script with two inputs, 5 and 0

Conclusion

That’s all about the wait command usage in Linux. As you have experienced, it can be put to a nice use in automation workflow. How do you use the command? Did you find additional uses than covered in this article? Do share your thoughts with our readers.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.