Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
No Result
View All Result
Home Linux

How to Use Bash wait Command in Linux

by Hollie Moore
4 months ago
in Linux
Bash Wait Command
ShareTweetSendShare
Readers like you help support Cloudbooklet. When you make a purchase using links on our site, we may earn an affiliate commission.

Use the powerful 'Bash wait' command to optimize shell programs. Synchronize background processes to enable orderly execution and error reduction. Tasks may be easily streamlined.

ADVERTISEMENT

In the world of shell scripting, process management and synchronization are critical. The Bash wait command is a quick and easy way to coordinate and regulate the execution flow of several processes. Understanding how to use the wait command correctly can improve the efficiency and dependability of your shell scripts greatly. In this article, we will deconstruct the Bash wait command, looking at its features, typical use cases, and best practices in Linux.

Table of Contents

  1. Prerequisites
  2. Syntax for Bash wait command
  3. Wait command
  4. Single Process Wait
  5. Single Process Bash wait
  6. Multiple Processes Bash Wait
  7. Multiple Processes Bash Wait With PID
  8. Conclusion

Prerequisites

  • Access to the command line/terminal.
  • Administrator (sudo) privileges.
  • A text editor, such as Vim or Nano.

Syntax for Bash wait command

SyntaxDescription
waitWaits for a child process to terminate.
wait [-n] job_idWaits for the job with the specified job_id to terminate. The -n option causes wait to return immediately if the job is already terminated.
wait -nWaits for the next background job to terminate.
Bash wait Command Syntax

Wait command

When working with wait in bash scripts, there are three more parameters to be aware of:

  1. The ampersand sign (&) following a command denotes a background job.
  2. $! returns the PID of the most recent background process. When working with several background processes, remember to save the previous PID in a variable.
  3. $? displays the last process’s exit status.

To show how these three factors interact, launch a terminal window and type:

ADVERTISEMENT
sleep 10 &
echo $!
echo $?
cloudbooklet@cloudbooklet:~$ sleep 10 &
[1] 15680 -
cloudbooklet@cloudbooklet:~$ echo 1
15680
cloudbooklet@cloudbooklet:~$ echo
0
cloudbooklet@cloudbooklet:~$

The $! argument holds the background process PID, while $? stores the exit status. The exit status 0 indicates that the command was successful.

You might also like

Symbolic Links In Linux

Symbolic Links in Linux: What They Are and How to Use Them

2 months ago
Ubuntu Password

Reset your Forgotten Ubuntu Password in 2 Minutes

2 months ago

Single Process Wait

Begin by opening the terminal and running the following background process:

sleep 10 &

Confirm that the job is executing in the background by typing:

ADVERTISEMENT
jobs -l

Note: If the job is marked as finished, consider increasing the sleep time to more than 10 seconds.

Use the wait command without any parameters to pause until the procedure is finished:

ADVERTISEMENT
wait
cloudbooklet@cloudbooklet:~$ sleep 10 &
[1] 49866
cloudbooklet@cloudbooklet:~$ jobs -l
[1]+ 49866 Running                                sleep 10 &
cloudbooklet@cloudbooklet:~$ wait

The terminal is waiting for the background process to complete.

cloudbooklet@cloudbooklet:~$ sleep 10 &
[1] 49866
cloudbooklet@cloudbooklet:~$ jobs -l
[1]+ 49866 Running                sleep 10 &
cloudbooklet@cloudbooklet:~$ wait
[1]+ Done               sleep 10 &            

The terminal prints the Done message after 10 seconds (due to sleep 10).

ADVERTISEMENT

Single Process Bash wait

Use the wait command to specify when a background process within a script must execute.

  1. For example, in a text editor, enter the following code:
#!/bin/bash

echo Background process &
echo First message
echo Second message
wait
echo Third message
~
~

If the background process does not complete the first and second processes, the wait command causes a pause to wait for the background process to finish the second process before proceeding to the third process.

ADVERTISEMENT
  1. Save the script as single_process.sh. Change the permissions in the terminal to make the script executable:
sudo chmod +x single_process.sh
  1. Use the following commands to run the script:
./single_process.sh
cloudbooklet@cloudbooklet:~$ ./single_process.sh
First process
second process
Background process 
Third process
cloudbooklet@cloudbooklet:~$

The background process finishes once the wait instruction is executed, and the script proceeds.

Multiple Processes Bash Wait

  1. Open a text editor and paste the following script with several processes:
#!/bin/bash

sleep 10 &
sleep 15 &
sleep 5 &
echo $(date +%T)
wait 
echo $(date +%T)
~
~

The script prints the current time before and after the wait command. Without any parameters, the application just waits for all processes to complete.

  1. Close the file and save the script as test.sh. Make the script executable next:
sudo chmod +x test.sh
  1. Finally, execute the program with:
./test.sh
cloudbooklet@cloudbooklet:~$ ./test.sh
14:32:22
14:32:37
cloudbooklet@cloudbooklet:~$

Because the operations are running in the background, all three are finished in fifteen seconds.

  1. Run the same script to test the following use cases:
  • Add the -n argument to wait. Only the fastest process completes, and the script stops in ten seconds.
  • Add the job ID to specify which job the script should wait for. Wait%1 pauses for process 1 (sleep 10) to complete.

Multiple Processes Bash Wait With PID

  1. When working with many processes, utilize the PID to identify a process. The following example script illustrates a single use case:
#!/bin/bash

echo "Process 1 lasts for 2s" && sleep 2 &
PID=$!
echo "Process 2 lasts for 3s" && sleep 3 &
echo "Current time $(date +%T)"
wait $PID
echo "Process 1 ended at time $(date +%T) with exit status $?"
wait $!
echo "Process 2 ended at time $(date +%T) with exit status $?"
~
~
  1. The script should be saved as multi_wait.sh. Make the script executable by typing:
sudo chmod +x multi_wait.sh
  1. Run the script to see the results:
./multi_wait.sh
cloudbooklet@cloudbooklet:~$ ./multi_process.sh
Process 1 lasts for 2s
Process 2 lasts for 3s
Current time 15:56:22
Process 1 ended at time 15:56:24 with exit status ©
Process 2 ended at time 15:56:25 with exit status ©
cloudbooklet@cloudbooklet:~$

The script completes the first process in two seconds (due to sleep 2) and the second process in three seconds. Both operations run concurrently and take three seconds to complete.

Also read: You might also find useful our guide on How to Change Directory in Linux Using cd Command.

Conclusion

Finally, the Bash wait command proved to be an indispensable tool for managing process synchronization in shell scripts. It ensures orderly execution and reduces errors by halting execution until defined procedures are completed. Incorporating the wait command into your scripts enables you to enhance efficiency and effectively control concurrent processes. Please feel free to share your thoughts and feedback in the comment section below.

Tags: CentOSDebianUbuntu
ShareTweetSendShare
Hollie Moore

Hollie Moore

Greetings, I am a technical writer who specializes in conveying complex topics in simple and engaging ways. I have a degree in computer science and journalism, and I have experience writing about software, data, and design. My content includes blog posts, tutorials, and documentation pages, which I always strive to make clear, concise, and useful for the reader. I am constantly learning new things and sharing my insights with others.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

&Quot; Systemd Service On Linux

How to Create a New systemd Service on Linux: A Step-by-Step Guide

3 months ago
List Groups In Linux

How to List Groups in Linux: A Guide for Beginners

3 months ago
Hostname In Linux

How to Modify the Hostname in Linux

3 months ago
Linux Systems

Linux systems Hacked with OpenSSH Malware

3 months ago

Follow Us

Trending Articles

Free Dating Sites For Men

Top 7 Free Dating Sites for Men in 2023

September 15, 2023

Microsoft Editor vs Grammarly: Which is the Best Grammar Checker?

How to Block YouTube Ads on Android TV in 2023 (6 Easy Methods)

Google Bard Extensions: How to Link Your Gmail, Docs, Maps, and More to an AI Chatbot

10 Best AI Prompts for Writers to Improve Website SEO

How to Create and Customize Stunning Contact Poster on iPhone

Popular Articles

Ai Annotation Jobs

AI Annotation Jobs: Everything You Need to Know

September 18, 2023

10 Best Email Autoresponder Tool for Lead Generation

Top 10 Remove Object from Photo Tools and Apps Online Free

How to Find Songs on YouTube by Humming the Tune

How to Use Midjourney for Pixel Graphics – Tips and Tricks

Microsoft Bing AI Image Generator: How to Create Amazing Artworks with AI

Subscribe Now

loader

Subscribe to our mailing list to receives daily updates!

Email Address*

Name

Cloudbooklet Logo

Welcome to our technology blog, where we explore the latest advancements in the field of artificial intelligence (AI) and how they are revolutionizing cloud computing. In this blog, we dive into the powerful capabilities of cloud platforms like Google Cloud Platform (GCP), Amazon Web Services (AWS), and Microsoft Azure, and how they are accelerating the adoption and deployment of AI solutions across various industries. Join us on this exciting journey as we explore the endless possibilities of AI and cloud computing.

  • About
  • Contact
  • Disclaimer
  • Privacy Policy

Cloudbooklet © 2023 All rights reserved.

No Result
View All Result
  • News
  • Artificial Intelligence
  • Applications
  • Linux

Cloudbooklet © 2023 All rights reserved.