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

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

2 hours ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

3 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

7 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

8 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

9 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

10 hours ago