October 2014
M T W T F S S
« Mar    
 12345
6789101112
13141516171819
20212223242526
2728293031  
135

Refs

Categories

Archives

profile for slm on Stack Exchange, a network of free, community-driven Q&A sites

[one-liner]: Bash’s wait Command

I’ve been working with UNIX operating systems for 20+ years and I’m still amazed at how much I still don’t know about it. Recently at work I came across a script (Bourne) that made use of the wait command. Now if someone had asked me if a command like this existed I would probably have guessed yes, so I wasn’t completely shocked by it, but I was kinda surprised that I had never seen it actually used anywhere in the wild.

The wait command gives you the ability to block a script on the termination of another process or list of processes. This can come in handy when you want to make sure a program finished before you’re script moves on. Specifically for a program where it runs as a backgrounded process.

I like examples so here are a couple that demonstrate how wait works.

Example 1
1
2
3
4
5
6
7
8
9
10
# wait with a process id
 
# run the sleep command for 15 seconds backgrounded
% sleep 15 &
[1] 8546
 
# wait for sleep's process id ($!) to finish, and then echo "hi"
% wait $! && echo "hi"
[1]+  Done                    sleep 15
hi
Example 2

Additionally you can give wait the job id (%1, %2, etc.) instead.

1
2
3
4
5
6
7
# wait with a job id
 
% sleep 15 &
[1] 8551
% wait %1 && echo "hi"
[1]+  Done                    sleep 15
hi
Example 3

You can also drop the process id entirely if the wait command occurs right after the backgrounded command.

1
2
3
4
5
6
7
# wait without any argument
 
% sleep 10 &
[1] 8552
% wait && echo "hi"
[1]+  Done                    sleep 10
hi
Example 4

Finally you can tell wait to wait on more than one process.

1
2
3
4
5
6
7
8
9
10
# wait on 2 processes
 
% sleep 10 &
[1] 8570
% sleep 10 &
[2] 8571
% wait %1 %2 && echo "hi"
[1]-  Done                    sleep 10
[2]+  Done                    sleep 10
hi

For more information about the wait command check out the wait section of the Bash FAQ. Check out more about special variables in Bash.

NOTE: For further details regarding my one-liner blog posts, check out my one-liner style guide primer.

4 comments to [one-liner]: Bash’s wait Command

  • CAS

    Good guide to show howt he basis of this works, but it left me very confused at best due to some of the syntax. Using your method caused some funky errors for me…Here is an example of what ended up working. Its a bit more “quick and dirty” focused, but its exactly what I needed:

    1
    2
    3
    4
    
    echo First process &
    sleep 5 && echo Second Process
    wait %1 %2
    echo Finshed

    The first echo command is backgrounded but executes immediately. The combination sleep and echo process also runs immediately, but takes extra time due to the sleep command.

    You can see if you run the command how the wait interacts, as “Finished” won’t output until AFTER “Second Process”. I hope this a bit clearer for new users. Its also worth noting that after the second command, your next background task you might create is also referenced as “wait %1″

    Thanks again for a great starting place, it just made this backup script a pleasure to work with instead of a nightmare haha.

    Regards.

  • slmingol

    Odd. I’m really striving to make this blog have only real examples that I’ve tested myself pretty stringently before I post them. I’ve seen many blogs where the examples are badly broken 8-).

    My examples are verbatim of what I typed in a shell (bash) with the output that the commands returned. The only thing that I modified was that I put comments above some of the code examples, these show up with a hash, i.e. “#”, AND I put a percent sign, i.e. “%”, in front of the commands that I typed so that you could distinguish between what the command run vs. the output that was getting returned. I’m sure I just lost some folks with this so here’s an example to make it more concrete.

    1
    2
    3
    
    # This is a comment
    % ls
    <output from ls>

    I use this format when I want to show output from the command. When I don’t include any of the output, I drop the “%” sign in the code blocks.

    1
    2
    
    # This is a comment
    ls

    I wonder if this notation that I’m using is what is causing your problems? I try to use this type of notation consistently throughout all the examples on my site. This type of notation is fairly consistent with other types of notations I’ve seen in programming books etc.

  • Just Exactly what I needed. thanks

  • Sean

    I have a script that backgrounds a whole group of rsync commands to run in parallel and then I use this to easily make the script wait until they are done:

    wait `jobs -p`

    which makes use of another Bash job control feature.

    see: http://www.gnu.org/software/bash/manual/bashref.html#Job-Control

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>