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

What Is a Data Protection Platform and Why Your Organization Needs One

A data protection platform is a unified system that helps organizations discover, classify, monitor, and…

10 hours ago

How Swiss Privacy Rules Affect AI Companies Under GDPR

AI companies operating in or around Switzerland face a compliance challenge that most legal teams…

2 days ago

Swiss FADP vs EU GDPR: Key Differences for AI and Data Privacy

Two major data privacy laws now govern how organizations handle personal data across Europe; the…

3 days ago

Kali Linux Commands Cheat Sheet: Complete Quick Reference

This cheat sheet covers the essential Kali Linux commands every pentester and ethical hacker uses…

1 month ago

What I Wish I Knew Before Learning Malware Analysis and Reverse Engineering

When I first started learning malware analysis and reverse engineering, I thought the hardest part…

1 month ago

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 months ago