Wait command in Linux explained with examples

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. Syntax: wait [Processid] [jobid]

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.

echo "testing the wait command instance 1" &
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 $?
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 &
kill $procc_id
wait $procc_id
echo $procc_id is terminate

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

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 &
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.

Running the script with two inputs, 10 and 3
Running the script with two inputs, 5 and 0


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.

Related Articles


Please enter your comment!
Please enter your name here


Latest Articles


How to install and run Powershell on Fedora Linux

PowerShell built upon the .NET Core framework is a powerful open-source command-line shell developed and maintained by Microsoft. It is a cross-platform (Windows, macOS, and Linux) automation and configuration tool that works well with your existing tools. It includes a command-line shell and an associated scripting language.


More Articles Like This