- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bash wait Command with Examples
Introduction
The Bash shell is one of most widely used shells in Unix/Linux environment. One of its most useful commands is wait command. In this article, we will explore Bash wait command and its usage with examples.
What is Bash wait Command?
The wait command is a built-in Bash shell command that waits for termination of a background process. This means that Bash shell will pause execution until specified process has completed.
Usage of Bash wait Command
The basic syntax of wait command is as follows −
wait [n]
Here, 'n' is process ID of background process that we want to wait for. If no process ID is specified, then wait command will wait for all background processes to complete.
Wait for a Single Process
Let's say we have a script that runs a long-running command in background −
#!/bin/bash sleep 10 & echo "Command running in background..."
In this script, we are running 'sleep' command in background using '&' symbol. We then print a message to console. If we want to wait for 'sleep' command to complete before continuing with script, we can use wait command −
#!/bin/bash sleep 10 & echo "Command running in background..." wait echo "Command has completed!"
In this updated script, we have added 'wait' command after starting 'sleep' command in background. This means that script will wait for 'sleep' command to complete before printing final message to console.
Wait for Multiple Processes
Sometimes, we may have multiple processes running in background that we want to wait for. In such cases, we can specify process IDs of each of processes that we want to wait for −
#!/bin/bash sleep 5 & sleep 10 & sleep 15 & echo "Commands running in background..." wait %1 %2 %3 echo "All commands have completed!"
In this script, we are running three 'sleep' commands in background, each with a different duration. We then print a message to console. To wait for all three commands to complete, we specify their process IDs as arguments to wait command using '%' symbol.
Handling Errors
If we try to wait for a process that does not exist, wait command will return an error. We can handle this error using '||' operator −
#!/bin/bash sleep 10 & echo "Command running in background..." wait || echo "Process not found!" echo "Command has completed!"
In this script, we are running 'sleep' command in background and waiting for it to complete using wait command. If 'sleep' command does not exist, then wait command will return an error. We can handle this error using '||' operator, which will execute command on right-hand side of operator only if command on left-hand side returns a non-zero exit code.
Using wait with Pipelines
The wait command can also be used with pipelines. For example, if we have a pipeline of two commands, we can wait for second command to complete before continuing with script −
#!/bin/bash echo "Starting pipeline..." echo "hello world" | grep "world" & wait %1 echo "Pipeline has completed!"
In this script, we have a pipeline that consists of two commands: 'echo "hello world"' and 'grep "world"'. We run pipeline in background using '&' symbol, and then wait for second command (the 'grep' command) to complete using wait command.
Using wait in a Loop
We can also use wait command in a loop to wait for multiple background processes to complete. For example, suppose we have a script that starts multiple background processes in a loop −
#!/bin/bash for i in {1..5} do sleep $i & done echo "Background processes started..." wait echo "All processes have completed!"
In this script, we start five 'sleep' commands in background using a loop. We then print a message to console and wait for all background processes to complete using wait command. Once all processes have completed, we print a final message to console.
Using wait with Command Substitution
We can also use wait command with command substitution. For example, suppose we have a script that generates a list of background processes using command substitution −
#!/bin/bash processes=$(for i in {1..3}; do sleep $i & echo $!; done) echo "Background processes started: $processes" wait $processes echo "All processes have completed!"
In this script, we use command substitution to generate a list of background process IDs. We start background processes using a loop, and then use 'echo $!' command to print process ID of each process. We then use wait command to wait for all background processes to complete, using list of process IDs generated by command substitution. Once all processes have completed, we print a final message to console.
Using Timeout with wait
The wait command can also be used with timeout command to limit amount of time we wait for a process to complete. For example, suppose we have a script that starts a long-running process in background, and we want to wait for it to complete, but we also want to limit amount of time we wait −
#!/bin/bash sleep 20 & echo "Command running in background..." timeout 10s wait if [ $? -eq 0 ]; then echo "Command has completed!" else echo "Command timed out!" fi
In this script, we start a 'sleep' command in background with a duration of 20 seconds. We then print a message to console and use timeout command to limit amount of time we wait for 'sleep' command to complete to 10 seconds. If 'sleep' command completes within time limit, we print a message to console indicating that it has completed. If command times out, we print a message indicating that command has timed out.
Using wait with Trap
We can also use wait command with trap command to handle signals and terminate background processes when necessary. For example, suppose we have a script that starts a background process and we want to terminate it if user presses Ctrl+C −
#!/bin/bash trap "kill %1" SIGINT sleep 20 & echo "Command running in background..." wait echo "Command has completed!"
In this script, we use trap command to handle SIGINT signal, which is sent when user presses Ctrl+C. We specify 'kill %1' command to terminate background process when signal is received. We then start 'sleep' command in background and wait for it to complete using wait command. Once command has completed, we print a message to console.
Conclusion
The Bash wait command is a useful tool that allows us to wait for completion of background processes. We can use it to ensure that our scripts run in a predictable and orderly manner, and to handle errors when processes do not complete as expected. By understanding basics of wait command and its usage, we can improve reliability and functionality of our Bash scripts.