Wait Command in Linux

28/12/2020
Chưa phân loại
wait is a built-in command of Linux that waits for completing any running process. wait command is used with a particular process id or job id. When multiple processes are running in the shell then only the process id of the last command will be known by the current shell. If wait command is executed this time, then it will be applied for the last command. If no process id or job id is given with wait command then it will wait for all current child processes to complete and returns exit status.

The exit status value of wait command depends on the command indicated by the last operand specified. When any process terminates abnormally then the exit status will be greater than 128 and shall be different from the exit status values of other commands. wait command exits with the  value 0 when it calls with no operands and all process IDs are known by the current shell have terminated. If wait command detects any error then it returns any value from 1 to 126. If the last process id is unknown then wait command exits with value 127. How you can use wait command in Linux is shown in this tutorial.

Example-1: Using wait command for multiple processes

After executing the following script, two processes will run in the background and the process id of the first echo command is stored in $process_id variable. When wait command is executed with $process_id then the next command will wait for completing the task of the first echo command. The second wait command is used with ‘$!’ and this indicate the process id of the last running process. ‘$?’ is used to read the status value of wait command.

#!/bin/bash
echo "testing wait command1" &
process_id=$!
echo "testing wait command2" &
wait $process_id
echo Job 1 exited with status $?
wait $!
echo Job 2 exited with status $?

Output:

$ bash wait1.sh

Example-2: Test wait command after using kill command

In the following script, wait command is executed after terminating the process. sleep command is running as a background process and kill command is executed to terminate the running process. After that wait command is executed with the process id of the terminated process. The output will show the process id of the terminated process.

#!/bin/bash
echo "Testing wait command"
sleep 20 &
pid=$!
kill $pid
wait $pid
echo $pid was terminated.

Output:

$ bash wait2.sh

Example-3: Check the exit status value

In the following script, the function check() is called by two argument values. It is discussed in the starting of the tutorial that if wait command executed successfully the exit value will 0 and if wait command detects any error then it will returns any value between 1 and 126. After running the script, if you pass 0 as second argument value then wait command terminates successfully and if you pass any value more than zero then it terminates unsuccessfully.

#!/bin/bash
function check()
{
echo "Sleep for $1 seconds"
sleep $1
exit $2
}
check $1 $2 &
b=$!
echo "Checking the status"
wait $b && echo OK || echo NOT OK

Output:

$ bash wait3.sh 3 0
$ bash wait3.sh 3 5

Hope, this tutorial will help to learn the use wait command properly. There is another command in Linux, named sleep to wait for the certain amount of times but there are some differences between these commands. If you are interested to know about sleep command then you can visit this link.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

LDAP [Phần 2] – Triển khai dịch vụ LDAP trên Onet Onet

Trong phần trước, Onet đã hướng dẫn các bạn triển khai LDAP trên CentOS 7. Trong bài này, mình sẽ...
30/12/2020

How to install Babe Music Player – A New Tiny Qt Music Player

Babe Music Player is a promising new tiny Qt Babe music player to keep your favorite songs at hand. Babe will handle all...
28/12/2020

How to Install and Configure KDE on CentOS 7

In the CentOS 7 installer, when you choose Server with GUI, GNOME desktop environment is installed by default. But you...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024