Wait Command in Linux with Examples

linux wait command

Wait is a shell command that waits for a given process to complete, then returns its exit status. Wait command is used to wait either particular process ID and job ID and return their termination status.

When executing the big automation flow we need to make few modules should wait until the previous set of module complete and return data pipelined into next modules, in this case we can use Wait command until complete previous module.

How wait command works

Wait command used to monitor the previous process, depends on previous process return status it will return the exit status. For example, if we want to wait to complete a particular process ID 13245, then we should use “wait 13245“ when process 13245 complete wait command return the return values of 13245 exit status.

- wait PID (PID - Process ID of a command, for which the utility is to wait for the termination).
- wait JID (JID - Job ID that identifies a background process to wait for, applicable only for invocations of wait in the current shell execution environment).

The exit status value of wait command depends on the last PID/JID specified. When any process terminates abnormally then the exit status will be greater than 128.

Wait command exits with the value 0 when it calls with no child process and all process IDs are known by the current shell have terminated. If wait command detects any error then it returns any value from 1 to 126. If the last process id is unknown then wait command exits with value 127.

Wait Command Examples

Let us check a few scripts to understand how wait command works.

Example: 1 – Script with Wait command

We have two scripts called 'foo.sh' and 'bar.sh' script. 'Foo.sh' script print numbers between 1 to 5 and 'bar.sh' script will call the foo.sh and run it in the background, get the PID of foo.sh and wait for it to complete, once completes it will start 'bar.sh' loop and complete.

Script – foo.sh

#!/bin/bash
for i in 1 2 3 4 5
do
      echo “foo.sh – Looping … number $i”
done

Script – bar.sh

#!/bin/bash
echo “Started bar.sh”
echo “Started foo.sh”
./foo.sh &
pid=$!
wait $pid
echo “Completed foo.sh”

for I in 1 2 3 4 5
do
        echo “bar.sh – Looping … number $i”
done

Result

Started bar.sh
Started foo.sh
foo.sh – Looping .. number 1
foo.sh – Looping .. number 2
foo.sh – Looping .. number 3
foo.sh – Looping .. number 4
foo.sh – Looping .. number 5
Completed foo.sh
bar.sh – Looping .. number 1
bar.sh – Looping .. number 2
bar.sh – Looping .. number 3
bar.sh – Looping .. number 4
bar.sh – Looping .. number 5
Completed bar.sh
$

Example 2 – Script without wait command

We have two scripts called 'foo.sh' and 'bar.sh' script. 'foo.sh' script print numbers between 1 to 5 and bar.sh script will call the foo.sh and run it in background but it will not wait for foo.sh to complete and execute both scripts.

Script – foo.sh

#!/bin/bash
for i in 1 2 3 4 5
do
      echo “foo.sh – Looping … number $i”
done

Script – bar.sh

#!/bin/bash
echo “Started bar.sh”
echo “Started foo.sh”
./foo.sh &
echo “Completed foo.sh”

for I in 1 2 3 4 5
do
        echo “bar.sh – Looping … number $i”
done

Result

Started bar.sh
Started foo.sh
Completed foo.sh
bar.sh – Looping .. number 1
bar.sh – Looping .. number 2
bar.sh – Looping .. number 3
bar.sh – Looping .. number 4
bar.sh – Looping .. number 5
Completed bar.sh
$
foo.sh – Looping .. number 1
foo.sh – Looping .. number 2
foo.sh – Looping .. number 3
foo.sh – Looping .. number 4
foo.sh – Looping .. number 5
$

Example: 3 – Script with wait command & return status

'bar.sh' script will call the foo.sh and run it in background, get the PID of foo.sh and wait for it to complete, once it complete it will start bar.sh loop and complete, Finally, it return the exit code of foo.sh script.

Script – foo.sh (Exit status = 0)

Script - foo.sh

#!/bin/bash
for i in 1 2 3 4 5
do
      echo “foo.sh – Looping … number $i”
done

Script – bar.sh

#!/bin/bash
./foo.sh &
BPID=$!
wait $BPID
stat=$?

if [ $stat –eq 0 ]
then
       echo “Exit status - $stat”
else
        echo “Exit status - $stat”
fi

Result

foo.sh – Looping .. number 1
foo.sh – Looping .. number 2
foo.sh – Looping .. number 3
foo.sh – Looping .. number 4
foo.sh – Looping .. number 5
Exit status - 0
$

Script – foo.sh (Exit status = NON Zero)

Script - foo.sh

#!/bin/bash
for i in 1 2 3 4 5
do
      iiecho “foo.sh – Looping … number $i”
done

Script – bar.sh

#!/bin/bash
./foo.sh &
BPID=$!
wait $BPID
stat=$?

if [ $stat –eq 0 ]
then
       echo “Exit status - $stat”
else
        echo “Exit status - $stat”
fi

Result

./foo.sh: line 4: iiecho: command not found
./foo.sh: line 4: iiecho: command not found
./foo.sh: line 4: iiecho: command not found
./foo.sh: line 4: iiecho: command not found
./foo.sh: line 4: iiecho: command not found
Exit status – 127
$

Conclusion

Wait and Sleep both are time-based system calls in the Operating system. Let's check the difference between wait and sleep command.

Wait: When user wants to halt the current process and it releases all the resources hold by the process and waits for some other process to execute. We need to use notify to make this process aware that start execution again upon completion of other process.

Sleep: This system call is used when user wants to halt the current process for some time. It keeps the locks hold on resources till the sleep time is over and again starts the execution of the process. Here process has control throughout the execution. e.g I executed some command on bash and want to sleep for some time since expecting some output from executed command which will be utilized in further execution of current process.

Bobbin Zachariah 11:53 am

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

3 Comments

  1. wait neither sleep are Linux commands, wait is a BASH built command and sleep is part of GNU coreutils, both BASH and coreutils arepart of GNU, then wait and sleep are GNU commands no Linux commands.