We use the wait command to wait for processes/jobs to change their state in Linux. This command waits for a process to change its status and then it returns the exit status.
Table of contents.
- Command parameters.
- Waiting for a single process.
- Waiting for multiple processes.
- Waiting for all processes.
- Waiting for a single job
In Linux, we use the wait command to wait for a process to terminate then print its exit status. This is very useful in cases where the exit status of a predecessor command determines the execution of the following command.
In this article, we learn about this command through various examples.
We write wait commands using the following format:
$ wait [-fn] [-p var] [id ...]
The command is followed by a process ID which is represented by ID in the syntax above.
Executing the wait without any options will make a script wait for all background processes to finish before execution continues.
When we execute the wait command with a PID as its argument, the process whose ID we use will wait for a process to terminate before it proceeds with its execution.
The -n option will wait for all background processes to complete and return their exit status before proceeding
The following are parameters we use with the wait command;
The & written after a command is used to execute a command as a background job. For example to execute the sleep command in the background we write:
$ sleep 10 &
Usually, when we execute the sleep command, the terminal pauses until the specified time ends. In this case, the sleep command is executed in the background and we can use the terminal to execute other commands.
We have said that the above command runs as a background job, to get its process ID(PID), we echo the value of $!. This gets the PID of the last background process.
Finally the $? parameter, prints the exit status of the last process.
When multiple processes are running, the process initiated by the last command is known.
If it returns 0, this means that the process was executed and terminated successfully, otherwise, if the exit status is over 128, it means that a process terminated abnormally.
Waiting for a single process.
To begin, we create a process and use its process ID with the wait command. After it is done executing, we print its exits status.
All this is handled by the following script:
#!/bin/bash # start a ping process as a background job ping -c 4 22.214.171.124 > file.txt & # print its PID echo "ping PID: $!" # wait for it to terminate wait $! # print exit status echo "ping exit status: $?"
In the above example, we start the ping process, which tests if the host 126.96.36.199 is reachable then sends the command output to the file file.txt.
In Linux, the exit status 0 means that a process was successful.
We can use a bogus IP address or another that does not work to get another exit status.
Waiting for multiple processes.
We can also use start multiple processes and check print their statues after they terminate:
#!/bin/bash # ping a working IP ping -c 2 188.8.131.52 > f1.txt & # store its PID in a variable PID1=$! # ping an IP that doesn't work ping -c 2 184.108.40.206 > f2.txt & # store its process ID PID2=$! # wait for and print the first process ID wait $PID1 echo Job1 status: $? # wait for and print the second process ID wait $PID2 echo Job2 status: $?
In this example, we start a ping process that returns an exit status indicating it is successful. We also start another process and wait for its exit status and print it.
We store the process IDs of both processes in variables PID1 and PID2.
Waiting for all processes.
Give the following script:
#!/bin/bash # multiple processes ping -c 2 google.com > f1.txt sleep 2 & ping -c 2 yahoo.com > f2.txt # wait for all processes to terminate wait -f echo "Status: $?"
In this example we use the -f command which waits for all processes to terminate then it prints the exit status.
Waiting for a single job.
We use the -n option with the wait command to wait for a single job to exit, that is, if we have multiple jobs running in the background, and use the -n option with the wait command. It will print the exit status of the first job to terminate. An example
#!/bin/bash # start the first job sleep 10 & # start the second job sleep 1 & # wait for the first job to terminate wait -n echo First job is done wait echo All jobs are done
In this example, all jobs run simultaneously, the wait command will print the exit status of the first job to terminate. In this case, after 1 second
The wait command waits for jobs to finish then prints their exit status. We can use the exit status of a command to determine the next steps.
In this article at OpenGenus, we learned how to use the wait command in Linux, we saw how to wait from a single process, wait for multiple processes, wait for the first process that produces an exit status, and run processes in the background.
For a comprehensive manual of the wait we execute the command, $ man wait in a Linux terminal.