How to use the command 'wait' (with examples)

How to use the command 'wait' (with examples)

The ‘wait’ command allows you to pause the execution of a shell script or command until a process or set of processes finish executing. It is commonly used in shell scripting to synchronize the execution flow, ensuring that certain processes have completed before moving ahead. This article will illustrate two use cases of the ‘wait’ command.

Use case 1: Wait for a process to finish given its process ID (PID) and return its exit status

Code:

wait pid

Motivation: In a shell script, you may have spawned multiple processes in the background and want to wait for a specific process to finish before proceeding. By using the ‘wait’ command with the process ID (PID) as an argument, you can achieve this synchronization.

Explanation: The ‘wait’ command is followed by the process ID (PID) of the process you want to wait for. The command will block the execution until the specified process completes and returns its exit status.

Example output:

Suppose we have a shell script that starts three processes in the background using ‘sleep’ command. We want to wait for one of them (say, with PID 1234) to finish before continuing with the remaining code.

#!/bin/bash

sleep 5 &
sleep 10 &
sleep 15 &

wait 1234

echo "Process with PID 1234 has finished"

# Remaining code...

In this example, the script waits until the process with PID 1234 completes before printing “Process with PID 1234 has finished” and executing the remaining code.

Use case 2: Wait for all processes known to the invoking shell to finish

Code:

wait

Motivation: Sometimes, it is useful to wait for all processes launched from a shell script to complete before terminating the script. This use case ensures proper synchronization and prevents premature termination.

Explanation: When the ‘wait’ command is used without any arguments, it waits for all child processes known to the invoking shell to finish. It is particularly useful when you have spawned multiple processes in the script using the background execution feature.

Example output:

Consider the following shell script that starts three processes in the background using ‘sleep’ command. We want to wait for all of them to finish before terminating the script.

#!/bin/bash

sleep 5 &
sleep 10 &
sleep 15 &

wait

echo "All processes have finished"

# Remaining code...

In this example, the script waits until all three processes complete before printing “All processes have finished” and executing the remaining code.

Conclusion:

The ‘wait’ command is a handy tool in shell scripting to synchronize the execution flow of processes. Whether you need to wait for a specific process or all processes to finish before proceeding, the ‘wait’ command can ensure proper synchronization in your scripts.

Related Posts

How to use the command 'tmuxinator' (with examples)

How to use the command 'tmuxinator' (with examples)

Tmuxinator is a command-line tool that helps in creating and managing tmux sessions easily.

Read More
How to use the command "sqlite3" (with examples)

How to use the command "sqlite3" (with examples)

The command “sqlite3” is the command-line interface to SQLite 3, which is a self-contained file-based embedded SQL engine.

Read More
How to use the command `ansiweather` (with examples)

How to use the command `ansiweather` (with examples)

The ansiweather command is a shell script that allows you to display the current weather conditions in your terminal.

Read More