Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In this Linux tip, we’re going to look at the wait command. It's a command that allows you to wait for a process running in the background to finish before you move on to the next command. Here's a simple example of how it works. If you run a sleep command in the background:
And then use wait to pause until it completes, you'll have to wait a number a few seconds for your prompt to return.
You can do the same thing in a script when you want to wait for a process to complete before you move to the next step. Say you want to produce some kind of report and then use it in your script. You might start your script like this:
The $! In the second line represents the process ID for the most recently backgrounded process. The wait command will then wait for the report to finish before it moves on to the remaining lines in the script.
If you use wait with a -n instead of a process or job ID, it will wait for the first of any number of your background processes to complete.
In this example, the sleep 6 command will finish before the sleep 12, so the first wait command will wait for its completion and the second wait will for the sleep 12.
That’s your Linux tip for the wait command.
If you have questions or would like to suggest a topic, please add a comment below. And don’t forget to subscribe to the IDG Tech(talk) channel on YouTube.
If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and NetworkWorld.com.