Bash waits for background processes
This article explains how to wait for background processes in Bash.
Bash waits for background processes
The wait command in Bash can be used to wait for all background processes to complete. This command will wait for the process and return the exit status.
wait
Commands affect the current shell execution environment, which is why they are built into shells such as Bash.
wait
The syntax of the command is:
wait [Options] BackgroundProcessID
Where BackgroundProcessID id is a process or job. The wait command will wait until all processes and subprocesses of the given ID have completed.
A simple example of a wait command could be:
wait 1234
The above command will wait for the background process 1234. We can also give multiple processes, and then the wait command will wait for all the background processes.
Now let's run a process in the background:
rsync -a /mn/c/Users/Sheeraz &
The above command will run a process in the background and return the job and process ID. See the output:
[1] 37
The job ID is 1 and the process ID is 37.
Now to wait for the job, we run the following command:
wait %1
where 1 is the job ID. The output of this command is:
[1]+ Exit 23 rsync -a /mn/c/Users/Sheeraz
To wait for this process, we use the following command:
wait 37
The above command will wait for the background process 37 to complete. Most of the time, wait
the command is used in Bash script files to spawn background processes for parallel execution.
The following is an example of a script file:
#!/bin/bash
sleep 30 &
ProcessId=$!
echo "PID: $ProcessId"
wait $ProcessId
echo "Exit status: $?"
Running this script will output the process ID and exit status of the background process. View the output:
PID: 50
Exit status: 0
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Hosting Docker Internal in Linux
Publish Date:2025/03/23 Views:143 Category:OPERATING SYSTEM
-
Docker allows developers to efficiently build, test, and deploy applications by packaging them in standardized units called containers. When working with Docker containers, you may encounter scenarios where you need to connect a container t
Setting the working directory in Docker
Publish Date:2025/03/23 Views:198 Category:OPERATING SYSTEM
-
If present, the working directory of a process in Compute is a directory in a linked hierarchical file system that is dynamic for each process. In Docker, we can set our working directory by editing the Dockerfile and adding the key WORKDIR
How to get IP address in CentOS
Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM
-
This short article is a brief introduction to CentOS followed by a brief discussion on how we can get the server IP address in CentOS using the Command Line Interface (CLI). This article will discuss some of the commands and their usage for
Updating YUM in Linux
Publish Date:2025/03/23 Views:148 Category:OPERATING SYSTEM
-
This article will teach us how to update YUM in Linux and how to install, update, remove, find and manage packages on a Linux system. We have also seen the difference yum update between and in Linux yum upgrade . yum update command in Linux
Installing Deb Files in Linux
Publish Date:2025/03/23 Views:93 Category:OPERATING SYSTEM
-
In this Linux article, we will learn how to install .deb (Debian Package) files on Linux systems. We will also see how to remove .deb files after installation. More importantly, we will learn different ways to install .deb files on Linux sy
lsof Command in Linux
Publish Date:2025/03/23 Views:100 Category:OPERATING SYSTEM
-
In this Linux article, we will learn about lsof command in Linux operating system. We will see how to use this command for different purposes in Linux. We use lsof the lsof command to verify the ports in use on the Linux operating system. U
How to solve the problem of not being able to execute binary files in Linux
Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM
-
In this article, we will learn how to execute binary files in Linux. We will also learn how to troubleshoot the error if Linux fails to execute the binary file. Usually, this error occurs when we run shell scripts in Linux. This article wil
Error in Linux Mesg: Ttyname Failed: Inappropriate Ioctl for Device Error
Publish Date:2025/03/23 Views:178 Category:OPERATING SYSTEM
-
In this article, we will learn how to fix the error mesg: ttyname failed: Inappropriate ioctl for device in Linux . We will discuss some of the causes of this error and show you how to fix it. Let's start with what causes this error in Linu
ps aux command in Linux
Publish Date:2025/03/23 Views:57 Category:OPERATING SYSTEM
-
If you are using Linux and are looking for a tool that can monitor all the processes running on your system, then you should use the command ps aux. This command will show you an overview of all running processes. It is very useful for trou