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

How to Setup PostgreSQL 11 Replication

PostgreSQL is an open source relational database management system (RDBMS). It is one of the most advanced database out...
28/12/2020

Linux Kernel Tutorial for Beginners

Background info on Operating Systems An Operating System, abbreviated as OS, is a piece of software that controls the hardware...
28/12/2020

[CI/CD] Phần 3: Hướng dẫn tích hợp Jenkins và Gitlab

Chắc hẳn các bạn đã nghe tới 2 khái niệm CI và CD và muốn tự động hóa một phần hoặc hoàn...
30/12/2020
Bài Viết

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

Hướng Dẫn Chọn Dịch Vụ Thuê Địa Chỉ IPv4
10/03/2025

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

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