How To

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

Cyber Defence

Recent Posts

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

9 hours ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

DockerĀ is an open-source platform that lets you package and run applications inside containers. Each container…

20 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

20 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

21 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

21 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

22 hours ago