Bash Wait command in Linux

11

The wait is a Linux command that returns an exit status after waiting for a complete running process. When several processes are running simultaneously, the wait command can only keep track of the last one. If the wait command is not associated with a job or process ID, it will wait for all child processes to complete before returning an exit status. The bash wait command is often used with the process IDs or Job IDs command.

In this tutorial, we will explore the Bash Wait Command in Linux.

Syntax:

The general syntax of the Wait command in Linux is:

wait [option] ID

ID would be a process ID or Job ID.

Explaining Bash Wait Command in Linux:

First, create a file by using the touch command:

Make this file executable by using the below command:

Bash Wait command in Linux 1617258680 431 Bash Wait command in Linux

Once the executable privileges are granted to the file, open the file and write a script in bash file:

#!/bin/bash

sleep 3 &

processID=$!

echo “PID: $processID

wait $processID

echo “Exit status: $?”

Bash Wait command in Linux 1617258680 240 Bash Wait command in Linux

$! is a variable in BASH that stores the PID of the most recent process.

Now, run the script as follows:

$ ./filename

$ ./BashWait.sh

Bash Wait command in Linux 1617258680 627 Bash Wait command in Linux

Process ID and Exist status will appear in the shell.

Using –n option:

With the –n option, the wait command only waits for a single job from the provided process ID or job specs to finish before returning its exit status. Wait -n waits for any background job to finish and returns the job exit status if no arguments are provided.

Write the below-given lines in your script:

#!/bin/bash

sleep 30 &

sleep 8 &

sleep 7 &

wait -n

echo “First job has been completed.”

wait

echo “All jobs have been completed.”

Bash Wait command in Linux 1617258681 184 Bash Wait command in Linux

Next, run the script again and when the first job is completed, it will print the message on the terminal and wait for all other jobs to be completed.

Bash Wait command in Linux 1617258681 989 Bash Wait command in Linux

Using –f option:

The -f option waits for each process id or job to stop before returning the exit code. Job control is available only for responsive prompts by default.

Open terminal and run command:

Bash Wait command in Linux 1617258681 672 Bash Wait command in Linux

Wait for process 3944.

Bash Wait command in Linux 1617258682 457 Bash Wait command in Linux

Open a different terminal window and execute the kill command to terminate the process.

Bash Wait command in Linux 1617258682 443 Bash Wait command in Linux

The status will be changed. The Wait command will complete and return the process exit code.

Repeat the above-given steps with the –f command.

Script with Wait command:

We are using ‘hello.sh’ and ‘bash.sh’ scripts for demonstration. The ‘ hello.sh’ script prints numbers from 1 to 5, and ‘bash.sh’ script calls hello.sh and runs it in the background, having the PID of hello.sh and waiting for it to end.

Create two scripts with the name hello and bash:

Bash Wait command in Linux 1617258682 434 Bash Wait command in Linux

Add the below-given lines in the hello.sh file:

#!/bin/bash

for i in 1 2 3 4 5 6 7 8 9 10

do

      echo “hello.sh – Loop number $i.”

done

Bash Wait command in Linux 1617258683 187 Bash Wait command in Linux

Add the below-given lines in the bash script:

#!/bin/bash

echo “Started bash.sh”

echo “Started hello.sh”

./hello.sh &

process_id=$!

wait $process_id

echo “Completed hello.sh

Bash Wait command in Linux 1617258683 513 Bash Wait command in Linux

Output:

Bash Wait command in Linux 1617258684 335 Bash Wait command in Linux

Conclusion:

When a user wants to stop a process, the system releases all resources kept by the process and waits for another to start. We’ll need to notify the process that it can restart execution once the other processes are completed. The wait command in bash waits to complete the execution and returns the exit status when the process execution is complete. In this manual, we have seen some examples of the Bash wait command in Linux.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More