Previous Table of Contents Next


Module 152
wait

DESCRIPTION

The internal wait command causes the shell to wait for processes to complete before executing the next command. It is usually used to wait for background processes to finish executing. The shell itself waits for foreground processes to complete before executing the next command; therefore, you only need wait for background processing.

COMMAND FORMAT

Following is the general format of the wait command.

     wait [ PID ]

BSD (Berkeley)
wait

Arguments

The following argument may be passed to the wait command.

PID A specific process ID of a background job. The shell waits for that process to complete before continuing to process the next command.
If no PID is given then the shell waits for all background processes to finish executing before continuing to run the next command.

FURTHER DISCUSSION

If you have some processes that take a long time to execute, you may want them to run in the background. You may find that you need the information produced by one of these commands to perform another task. Thus you may want to wait for the background process or processes to complete before continuing. The wait command is used to handle this waiting process.

In shell scripts you may have multiple commands running in background mode. If this is the case you can wait for any one process or for all of them to complete. To wait for all of the background processes to complete you use:

     wait

with no arguments. If you wanted to wait for a specific process to complete you use:

     wait PID

where PID is the process ID of a given process. To know what process ID a process has you can use the shell variable $!. For example, if you have more than one process in the background and you need to wait on the second one to complete, you might use the following code:

     sort -u data > data.sorted &      PID1=$!
     lp report &      PID2=$!
     sort -u customer > temp1 &      PID3=$!
     spell memo > memo.mis &      wait $PID3      # wait for customer sort to complete
     mv temp1 customer
     wait $PID1      # wait for the sort to complete
     mv data.sorted data
     wait
     echo Misspelled words from your memo file:
     cat memo.mis

RELATED COMMANDS

Refer to the sleep command described in Module 121.

RETURN CODES

The return code of the specified PID is returned via wait. Therefore, you can check the status of wait to see if the process actually terminated correctly.

APPLICATIONS

The wait command is normally used in shell scripts to stop command execution until a certain process has completed. Once the command or commands have completed, the return code may be checked for proper process completion. Using the wait command is often useful when you can perform parallel processing in your shells but you need to guarantee sequential processing at some point in the script.

TYPICAL OPERATION

In this activity you use the wait command in a shell script to wait for a sort command to finish executing in the background. Begin at the shell prompt.

1.  Type find /usr/spool -name core > /dev/null & and press Return. The find command is placed in the background for processing.
2.  Now type wait $! to wait for the find to complete execution. Some systems may finish the background process before you can type the wait command. If this is your case feel lucky. But you should still use the wait command at times you must guarantee the proper sequence of command execution.

C Shell
Use wait without the $!.

3.  Turn to Module 142 to continue the learning sequence.


Previous Table of Contents Next