Wait command in Linux with examples

Wait is a shell command that waits for a given process to complete and then returns to its exit status. The Wait command is used to wait for a specific process ID and job ID and return its terminated status.

When executing a large-scale automation process, we need to make several modules wait until the previous set of modules is completed and return the pipeline to the next module’s data. In this case, we can use the Wait command until the previous module is completed.

How wait commands work

The wait command is used to monitor the previous process. Depending on the return status of the previous process, it will return to the exit status. For example, if we want to wait for the completion of a specific process ID 13245, we should use “wait 13245” when the process 13245 complete wait command returns a return value of 13245 exit status.

-Waiting for PID (PID-Process ID of the utility to wait for terminated command). -wait JID (JID-Identifies the job ID of the background process to wait for, only applicable to wait calls in the current Shell execution environment).

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

Wait for command to exit with value 0 When it has no child process calls and all process IDs known to the current shell have terminated. If the wait command detects any errors, it will return any value between 1 and 126. If the last process ID is unknown, the wait command will exit with a value of 127.

Example of wait command

Let’s check some scripts to understand how the wait command works.

Example: 1 – Script with “wait” command

We have two scripts called “foo.sh” and “bar.sh” scripts. The ‘Foo.sh’ script prints a number between 1 and 5, while the ‘bar.sh’ script will call foo.sh and run it in the background, get the PID of foo.sh and wait for it to complete, and once it finishes, it The “bar.sh” loop will start and finish.

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 – No script waiting for command

We have two scripts called “foo.sh” and “bar.sh” scripts. The ‘foo.sh’ script outputs a number between 1 and 5, and the bar.sh script will call foo.sh and run it in the background, but it won’t wait for foo.sh to finish 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 and return status

The “bar.sh” script will call foo.sh and run it in the background, get the PID of foo.sh and wait for it to complete. Once completed, it will start the bar.sh loop and complete, and finally it returns Exit code.

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
$

in conclusion

Wait and hibernate are both time-based system calls in the operating system. Let’s check the difference between wait and sleep commands.

Wait: When the user wants to stop the current process and release all resources held by the process and wait for other processes to execute. We need to use notify to let the process know that execution will start again after the other processes have completed.

Sleep: Use this system call when the user wants to pause the current process for a period of time. It will remain locked on the resource until the sleep time is over and then start the process again. Here, the process has control throughout its execution. For example, I have executed some commands on bash and want to sleep for a while because I expect to get some output from the executed commands, these outputs will be used in further execution of the current process.

Sidebar