Cybersecurity Updates & Tools

Bash Wait Command: How To Wait For Background Processes

Introduction

The Bash wait command is used to wait for background processes to finish before continuing the script. If you are learning Bash scripting, understanding the wait command is useful when running multiple tasks at the same time.

In Linux, you can run commands in the background using the & symbol. This allows the script to continue without waiting for that command to finish. However, sometimes you may want the script to pause until all background tasks are completed. This is where the wait command is helpful.

The wait command is commonly used in automation scripts, backup scripts, system monitoring, log processing, server tasks, and cybersecurity workflows.

What Is The Bash Wait Command?

The wait command waits for background jobs to complete.

Basic syntax:

wait

When used alone, wait waits for all background processes started from the current shell.

Example:

command1 &command2 &waitecho "All tasks completed"

Here, both commands run in the background, and the script waits until they finish.

Basic Bash Wait Example

Create a new Bash script:

nano wait-example.sh

Add the following code:

#!/bin/bashecho "Starting task 1..."sleep 3 &echo "Starting task 2..."sleep 5 &waitecho "Both background tasks are completed"

Save and run the script:

chmod +x wait-example.sh./wait-example.sh

In this example, sleep 3 and sleep 5 run in the background. The script waits until both are finished, then prints the final message.

Why Use Wait In Bash Scripts?

Without wait, a script may continue before background commands are completed.

Example:

#!/bin/bashsleep 5 &echo "Script finished"

This script prints Script finished immediately, even though the background task is still running.

Now use wait:

#!/bin/bashsleep 5 &waitecho "Script finished after background task"

This time, the script waits properly.

Wait For A Specific Process ID

You can wait for a specific background process using its process ID. In Bash, $! stores the process ID of the last background command.

Example:

#!/bin/bashsleep 10 &pid=$!echo "Waiting for process ID: $pid"wait $pidecho "Process completed"

This is useful when you want to track and wait for one specific task.

Cybersecurity Example: Run Multiple Checks

#!/bin/bashecho "Starting security checks..."ping -c 4 192.168.1.1 > ping-result.txt &pid1=$!ss -tuln > open-ports.txt &pid2=$!wait $pid1echo "Ping check completed"wait $pid2echo "Open port check completed"echo "All security checks completed"

This script runs two checks in the background and waits for both to finish.

Conclusion

The Bash wait command is useful when working with background processes. It helps control script execution by waiting for one or more background tasks to complete.

For beginners, learning wait is important for writing better automation scripts. It is useful for parallel tasks, backups, monitoring scripts, cybersecurity checks, and Linux system administration